Java Reference
In-Depth Information
F we expect the close method to be called on the stream . Now we need to denote
that we've finished declaring our expectations; we do this by calling the replay
method G . The rest is invoking the method under test H and asserting the expected
result I .
We also add another test to simulate a condition when we can't close the Input-
Stream . We define an expectation where we expect the close method of the stream to
be invoked J , and on the next line we declare that an IOException should be raised
if this call occurs .
As the name of the framework suggests, using EasyMock is easy, and you should use
it whenever possible. But to make you aware of the entire mocking picture, we'd like
to introduce another framework, so you have a better taste of what mocking is.
1)
7.6.2
Using JMock
So far we showed how to implement our own mock objects and how to use the Easy-
Mock framework. In this section we introduce the JM ock framework ( http://
jmock.org/ ), so that we can have a full view of the different mocking techniques. As in
the previous section, we start with a simple example: reworking listing 7.4 by means of
JM ock. See listing 7.17.
Listing 7.17
Reworking the TestAccountService test using JMock
[...]
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
B
C
@RunWith( JMock.class )
public class TestAccountServiceJMock
{
private Mockery context = new JUnit4Mockery();
D
E
private AccountManager mockAccountManager;
@Before
public void setUp()
{
mockAccountManager = context.mock( AccountManager. class );
}
F
@Test
public void testTransferOk()
{
final Account senderAccount = new Account( "1", 200 );
final Account beneficiaryAccount = new Account( "2", 100 );
G
H
context.checking( new Expectations()
{
{
oneOf( mockAccountManager ).findAccountForUser( "1" );
will( returnValue( senderAccount ) );
I
 
 
 
Search WWH ::




Custom Search