Source:
public void testGetMovedContentForHttp301And200() throws Exception { String originalResult = "this is original result"; String movedResult = "this is moved result"; HttpContentRetriever service = prepareValidService(); Header mockedHeader = mock(Header.class); when(mockedHeader.getName()).thenReturn("Location"); when(mockedHeader.getValue()).thenReturn("newUrl"); Header[] headers = new Header[] {mockedHeader}; 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.getAllHeaders()).thenReturn(headers); 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); assertNull(response.getException()); assertEquals(movedResult, response.getContent()); }

Target:
public void testGetMovedContentForHttp301And200() throws Exception { String originalResult = "this is original result"; String movedResult = "this is moved result"; HttpContentRetriever service = prepareValidService(); Header mockedHeader = mock(Header.class); when(mockedHeader.getName()).thenReturn("Location"); when(mockedHeader.getValue()).thenReturn("newUrl"); Header[] headers = new Header[] {mockedHeader}; 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.getAllHeaders()).thenReturn(headers); 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); assertNull(response.getException()); assertEquals(movedResult, response.getContent()); }