Testing servlets

2,247 views
Skip to first unread message

incognito_dev

unread,
Jan 8, 2011, 10:28:03 PM1/8/11
to mockito
Hi,

I am fairly new to mockito. Been trying to test a servlet for the past
two days and for the life of me i cannot do it.

Sorry to do a code dump here, but here's the simple method i am trying
to mock.

public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
try{
InputStream streaminput=request.getInputStream();
ObjectInputStream stream=new ObjectInputStream(streaminput);
String result=(String)stream.readObject();
System.out.println(result);

}catch (Exception e){
e.printStackTrace();
}

}


My biggest problem here is that as you cannot see i cannot inject
ObjectInputStream. I can't send in a real object because the stream is
then corrupted.

I have tried many different combinations no to avail.

Here is the test

@PrepareForTest(value = servlet.class)
public class servletTest extends servlet{

ObjectInputStream
mockInputStream=PowerMockito.mock(ObjectInputStream.class);

@Test
public void tesetSTuff() throws Exception {
ObjectInputStream
mockInputStream=PowerMockito.mock(ObjectInputStream.class);

servlet servletImpl = new servlet();
HttpServletRequest
request=PowerMockito.mock(HttpServletRequest.class);
HttpServletResponse
response=PowerMockito.mock(HttpServletResponse.class);

PowerMockito.whenNew(ObjectInputStream.class).withArguments(Mockito.any()).thenReturn(mockInputStream);
try {
servletImpl.doPost(request, response);
} catch (Exception e) {
e.printStackTrace();
}

}


Any pointers on this topic will be greatly appreciated.

Kristofer Karlsson

unread,
Jan 9, 2011, 5:49:08 AM1/9/11
to moc...@googlegroups.com
Why do you need to mock the ObjectInputStream?

Just use plain Mockito without PowerMock and do something like:

baos = new ByteArrayOutputStream();
os = new ObjectOutputStream(baos);
os.write("the string");

x = new ByteArrayInputStream(baos.toByteArray());
when(request.getInputStream()).thenReturn(x);


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.


incognito_dev

unread,
Jan 9, 2011, 7:15:50 AM1/9/11
to mockito
Thanks for the response.

That's one of the first things i tried, however i get this error from
Mockito.

The method thenReturn(ServletInputStream) in the type
OngoingStubbing<ServletInputStream> is not applicable for the
arguments (ByteArrayInputStream)

as public request.getInputStream returns a ServletInputStream
which led me to mock the ServletInputStream, however if i pass in a
mocked object to
ObjectInputStream stream=new ObjectInputStream(streaminput)
it just loops forever and hangs.

This prompted me to try and mock this constructor [new
ObjectInputStream(streaminput)]
to have some control over how it's instantiated and that leads me
where i am now.

PowerMockito.whenNew(ObjectInputStream.class).withParameterTypes(InputStream.class).
withArguments(Mockito.any(InputStream.class)).thenAnswer(new
Answer<ObjectInputStream>() {

@Override
public ObjectInputStream answer(InvocationOnMock invocation) throws
Throwable {

final ObjectInputStream
mockedInput=PowerMockito.mock(ObjectInputStream.class);

PowerMockito.when(mockedInput.readObject()).thenReturn("ByteArray");
return mockedInput;
}
});


java.lang.NullPointerException
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.test.servletTest$1.answer(servletTest.java:40)
at com.test.servletTest$1.answer(servletTest.java:1)

==================
where line 40 is:
PowerMockito.when(mockedInput.readObject()).thenReturn("ByteArray");

Thanks again and sorry for the long post, any help will be greatly
appreciated.


On Jan 9, 5:49 am, Kristofer Karlsson <kristofer.karls...@gmail.com>
wrote:
> > mockito+u...@googlegroups.com<mockito%2Bunsu...@googlegroups.com >
> > .

Moandji Ezana

unread,
Jan 9, 2011, 8:39:01 AM1/9/11
to moc...@googlegroups.com

If you're only calling one method on the request, you could just hand-roll the mock. If you're using Spring, it has a ready-made request mock.

Moandji

--
www.moandjiezana.com

Sent from my phone

> To unsubscribe from this group, send email to mockito+u...@googlegroups.com.

James Carr

unread,
Jan 9, 2011, 9:35:43 AM1/9/11
to moc...@googlegroups.com
This is one of the reasons I use springMVC, jersey, etc. Working with
servlets is pretty low level and an absolute pain. I'd avoid it if I
can, and even then I would probably only deal with them if I was
building a framework. These days I just don't see any reason to deal
with them with the slew of frameworks out there that abstract them
away and let me work with plain old java objects.

Thanks,
James

incognito_dev

unread,
Jan 9, 2011, 8:42:01 AM1/9/11
to mockito
Sorry not very familiar with the terminology what do you mean by "hand
rolling the mock"?

On Jan 9, 8:39 am, Moandji Ezana <mwa...@gmail.com> wrote:
> If you're only calling one method on the request, you could just hand-roll
> the mock. If you're using Spring, it has a ready-made request mock.
>
> Moandji
>
> --www.moandjiezana.com
>
> Sent from my phone
> <mockito%2Bunsu...@googlegroups.com<mockito%252Bunsubscribe@googlegroup s.com>>>> > .

Balázs Török

unread,
Jan 9, 2011, 8:19:10 AM1/9/11
to moc...@googlegroups.com
Hi,

You can wrap your ByteArrayInputStream in a custom ServletInputSream:


when(request.getInputStream()).thenReturn(createServletInputStream("the
string"));

public static ServletInputStream createServletInputStream(Object
object) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(baos );
os.writeObject(object);

final InputStream bais = new
ByteArrayInputStream(baos.toByteArray());

return new ServletInputStream() {

@Override
public int read() throws IOException {
return bais.read();
}
};
}

Balazs

incognito_dev

unread,
Jan 9, 2011, 11:51:46 AM1/9/11
to mockito
Hi

This worked! I really appreciate this. It makes sense now.

Moandji Ezana

unread,
Jan 9, 2011, 4:50:29 PM1/9/11
to moc...@googlegroups.com
On Sun, Jan 9, 2011 at 2:42 PM, incognito_dev <soli...@gmail.com> wrote:
Sorry not very familiar with the terminology what do you mean by "hand
rolling the mock"?

It means implementing the interface yourself. But I guess you don't need to any more. In some cases it's easier than using some of the library's more complicated features.

Moandji
Reply all
Reply to author
Forward
0 new messages