Java Reference
In-Depth Information
Well, with JM ock you don't have to do that; the JM ock JU nit runner takes care of this,
and in case any of the expected calls were not made, the test will fail.
Following the pattern from the previous section about EasyMock, let's rework list-
ing 7.12, shown in listing 7.18, showing the WebClient test, this time using JM ock.
Listing 7.18
Reworking the TestWebClient test using JMock
[...]
B
@RunWith( JMock. class )
public class TestWebClientJMock
{
private Mockery context = new JUnit4Mockery()
{
{
setImposteriser( ClassImposteriser.INSTANCE );
}
};
C
@Test
public void testGetContentOk() throws Exception
{
final ConnectionFactory factory =
context.mock( ConnectionFactory. class );
final InputStream mockStream =
context.mock( InputStream. class );
D
context.checking( new Expectations()
{
{
oneOf( factory ).getData();
will( returnValue( mockStream ) );
E
atLeast( 1 ).of( mockStream ).read();
will( onConsecutiveCalls(
returnValue( new Integer( ( byte ) 'W' ) ),
returnValue( new Integer( ( byte ) 'o' ) ),
returnValue( new Integer( ( byte ) 'r' ) ),
returnValue( new Integer( ( byte ) 'k' ) ),
returnValue( new Integer( ( byte ) 's' ) ),
returnValue( new Integer( ( byte ) '!' ) ),
returnValue( -1 ) ) );
F
oneOf( mockStream ).close();
}
} );
WebClient2 client = new WebClient2();
String result = client.getContent( factory );
G
H
assertEquals( "Works!", result );
}
@Test
public void testGetContentCannotCloseInputStream() throws Exception
{
 
Search WWH ::




Custom Search