Source:
public void testGetMovedContentForHttp301NoLocationHeader() throws Exception { String originalResult = "this is original result"; String movedResult = "this is moved result"; HttpContentRetriever service = prepareValidService(); CloseableHttpResponse getContentHttpResponse = mock(CloseableHttpResponse.class); { StatusLine getContentStatusLine = mock(StatusLine.class); HttpEntity getContentHttpEntity = mock(HttpEntity.class); when(getContentHttpResponse.getStatusLine()).thenReturn(getContentStatusLine); when(getContentStatusLine.getStatusCode()).thenReturn(301); when(getContentHttpResponse.getEntity()).thenReturn(getContentHttpEntity); when(getContentHttpEntity.getContent()).thenReturn(new ByteArrayInputStream(originalResult.getBytes())); } CloseableHttpResponse getMovedContentHttpResponse = mock(CloseableHttpResponse.class); { StatusLine getMovedContentStatusLine = mock(StatusLine.class); HttpEntity getMovedContentHttpEntity = mock(HttpEntity.class); when(getMovedContentHttpResponse.getStatusLine()).thenReturn(getMovedContentStatusLine); when(getMovedContentStatusLine.getStatusCode()).thenReturn(200); when(getMovedContentHttpResponse.getEntity()).thenReturn(getMovedContentHttpEntity); when(getMovedContentHttpEntity.getContent()).thenReturn(new ByteArrayInputStream(movedResult.getBytes())); } when(httpClient.execute(any(HttpGet.class))).thenReturn(getContentHttpResponse, getMovedContentHttpResponse); ContentRetrieverResponse response = service.retrieveUrlContent("someUrl"); assertNotNull(response); assertTrue(response.getException() instanceof RuntimeException); assertEquals("", response.getContent()); }

Target:
public void testGetMovedContentForHttp301NoLocationHeader() throws Exception { String originalResult = "this is original result"; String movedResult = "this is moved result"; HttpContentRetriever service = prepareValidService(); CloseableHttpResponse getContentHttpResponse = mock(CloseableHttpResponse.class); StatusLine getContentStatusLine = mock(StatusLine.class); HttpEntity getContentHttpEntity = mock(HttpEntity.class); when(getContentHttpResponse.getStatusLine()).thenReturn(getContentStatusLine); when(getContentStatusLine.getStatusCode()).thenReturn(301); when(getContentHttpResponse.getEntity()).thenReturn(getContentHttpEntity); when(getContentHttpEntity.getContent()).thenReturn(new ByteArrayInputStream(originalResult.getBytes())); CloseableHttpResponse getMovedContentHttpResponse = mock(CloseableHttpResponse.class); StatusLine getMovedContentStatusLine = mock(StatusLine.class); HttpEntity getMovedContentHttpEntity = mock(HttpEntity.class); when(getMovedContentHttpResponse.getStatusLine()).thenReturn(getMovedContentStatusLine); when(getMovedContentStatusLine.getStatusCode()).thenReturn(200); when(getMovedContentHttpResponse.getEntity()).thenReturn(getMovedContentHttpEntity); when(getMovedContentHttpEntity.getContent()).thenReturn(new ByteArrayInputStream(movedResult.getBytes())); when(httpClient.execute(any(HttpGet.class))).thenReturn(getContentHttpResponse, getMovedContentHttpResponse); ContentRetrieverResponse response = service.retrieveUrlContent("someUrl"); assertNotNull(response); assertTrue(response.getException() instanceof RuntimeException); assertEquals("", response.getContent()); }