Java Reference
In-Depth Information
@Test
public void testGetContentOk() throws Exception
{
expect( factory.getData() ).andReturn( stream );
expect( stream.read() ).andReturn( new Integer( (byte) 'W' ) );
expect( stream.read() ).andReturn( new Integer( (byte) 'o' ) );
expect( stream.read() ).andReturn( new Integer( (byte) 'r' ) );
expect( stream.read() ).andReturn( new Integer( (byte) 'k' ) );
expect( stream.read() ).andReturn( new Integer( (byte) 's' ) );
expect( stream.read() ).andReturn( new Integer( (byte) '!' ) );
expect( stream.read() ).andReturn( -1 );
stream.close();
E
G
F
replay( factory );
replay( stream );
WebClient2 client = new WebClient2();
String result = client.getContent( factory );
H
I
assertEquals( "Works!", result );
}
[...]
@Test
public void testGetContentCannotCloseInputStream() throws Exception {
expect( factory.getData() ).andReturn( stream );
expect( stream.read() ).andReturn( -1 );
stream.close();
expectLastCall().andThrow(new IOException("cannot close"));
J
1)
replay( factory );
replay( stream );
WebClient2 client = new WebClient2();
String result = client.getContent( factory );
assertNull( result );
}
@After
public void tearDown()
{
verify( factory );
verify( stream );
}
}
We start the listing by importing the objects that we need B . Notice that because we
use the classextensions extension of EasyMock, we now need to import the
org.easymock.classextension.EasyMock object instead of org.easymock.EasyMock .
That's it! Now we're ready to create mock objects of classes and interfaces using the
statically imported methods of classextensions . In C , as in the previous listings, we
declare the objects that we want to mock, and in D we call the createMock method to
initialize them.
In E we define the expectation of the stream when the read method is invoked
(notice that to stop reading from the stream , the last thing to return is -1) , and in
Search WWH ::




Custom Search