Tuesday, 15 June 2010

java - Stubbing Out a Null-Returning Function in Mockito -



java - Stubbing Out a Null-Returning Function in Mockito -

i've been using junit , mockito in effort test how particular networking module affects input , output streams. facilitate testing, i've created set of mock network object streams (i.e. instance of objectinputstream , instance of objectoutputstream) , stubbed out methods want testing in fashion similar next code snippet:

import java.io.objectinputstream; import java.io.objectoutputstream; import static org.mockito.mockito.*; import org.junit.test; public class networktester { @test public void modulerespondstoserverrequest() throws exception { objectinputstream mockclientin = mock(objectinputstream.class); objectoutputstream mockclientout = mock(objectoutputstream.class); when(mockclientin.readobject()).thenreturn("sent server"); // initialize module , connect network... verify(mockclientout, timeout(100).atleastonce()).writeobject(isnotnull()); } }

however, when seek compile , run code, null pointer exception @ line "when(mockclientin.readobject()).thenreturn("sent server");". believe caused fact "readobject()" function returning null pointer when called on stubbed object, don't know if it's possible bypass behavior.

is there way stub function (or similar null returning functions) using mockito? in advance help.

mockito cannot mock readobject() since final. seek powermockito or consider redesigning code easier testability. see give-and-take on topic here.

java mockito

No comments:

Post a Comment