Here is my code for servlet:
String foo = "foo";
Foo foo = Foofactory.getfoos(foo);
String locationURI;
String redirectURI = "http://" + request.getServerName() + ":" +request.getServerPort() + "/foo";
locationURI = foo.getRequestBuilder(redirectURI);
response.sendRedirect(locationURI);
Here I want to test the IOException
for response.sendRedirect(locationURI);
I cannot use Mockito when feature as it shows void method cannot be used.
@Test(expected = IOException.class)
public void testService_CheckIOException() throws ServletException, IOException {
when(mockFoofactory.getfoos(Matchers.eq("foo"))).thenReturn(mockfoo);
when(mockRequest.getServerName()).thenReturn("serverName");
when(mockRequest.getServerPort()).thenReturn(80);
when(mockfoo.getRequestBuilder(Matchers.eq("http://serverName:80foo")))
.thenReturn("locationURI");
Servlet.service(mockRequest, mockResponse);
//This line doesn't work in mockito
when(mockResponse).sendRedirect("locationURI")).thenThrow(IOException.class);
Is there anyway I can throw IOException
and test for send redirect? I want to test the IOException
and IllegalStateException
that sendRedirect
method throws.