concept EasyMock in category junit

appears as: EasyMock, EasyMock
JUnit in Action

This is an excerpt from Manning's book JUnit in Action.

8.6.1   Using EasyMock

EasyMock (http://easymock.org) is an open-source framework that provides useful classes for mocking objects. To work with it, we need to add to the pom.xml file the dependencies shown in listing 8.18.

Listing 8.18 The EasyMock dependencies from the pom.xml configuration file
<dependency>
   <groupId>org.easymock</groupId>
   <artifactId>easymock</artifactId>
   <version>2.4</version>
</dependency>
<dependency>
   <groupId>org.easymock</groupId>
   <artifactId>easymockclassextension</artifactId>
   <version>2.4</version>
</dependency>

While trying to introduce EasyMock, the developers at Tested Data Systems revise some of the mocks constructed in the previous sections. The start is simple: reworking the AccountService test from listing 8.2.

Listing 8.19 Reworking the TestAccountService test using EasyMock
[...]
import static org.easymock.EasyMock.createMock;                         #A
import static org.easymock.EasyMock.replay;                             #A
import static org.easymock.EasyMock.expect;                             #A
import static org.easymock.EasyMock.verify;                             #A
 
public class TestAccountServiceEasyMock
{
    private AccountManager mockAccountManager;                          #B
 
    @BeforeEach
    public void setUp()
    {
        mockAccountManager = createMock( “mockAccountManager”,
                                                AccountManager.class ); #C
    }
 
    @Test
    public void testTransferOk()
    {
        Account senderAccount = new Account( "1", 200 );                #D
        Account beneficiaryAccount = new Account( "2", 100 );           #D
 
        // Start defining the expectations
        mockAccountManager.updateAccount( senderAccount );              #E
        mockAccountManager.updateAccount( beneficiaryAccount );         #E
 
        expect( mockAccountManager.findAccountForUser( "1" ) )
                                     .andReturn( senderAccount );       #F
        expect( mockAccountManager.findAccountForUser( "2" ) )
                                     .andReturn( beneficiaryAccount );  #F
       
        // we’re done defining the expectations
        replay( mockAccountManager );                                   #G
 
        AccountService accountService = new AccountService();
        accountService.setAccountManager( mockAccountManager );        
        accountService.transfer( "1", "2", 50 );                        #H
 
        assertEquals( 150, senderAccount.getBalance() );                #I
        assertEquals( 150, beneficiaryAccount.getBalance() );           #I
    }
 
    @AfterEach
    public void tearDown()
    {
        verify( mockAccountManager );                                   #J
    }
}
JUnit in Action, Second Edition

This is an excerpt from Manning's book JUnit in Action, Second Edition.

7.6.1. Using EasyMock

EasyMock (http://easymock.org/) is an open source framework that provides useful classes for mocking objects. To use the framework you need to download the zip archive from the website of the project, unpack it somewhere, and include the contained easymock.jar in your classpath.

To show you how easy it is to construct mock objects in your test cases using EasyMock, we revise some of the mocks we constructed in the previous sections. We start with a simple one: reworking the AccountService test from listing 7.2. See listing 7.15.

Listing 7.15. Reworking the TestAccountService test using EasyMock

As you see, listing 7.15 is pretty much the same size as listing 7.4, but we are spared the writing of any additional mock classes. We start the listing by defining the imports from the EasyMock library that we need . EasyMock relies heavily on the static-import feature of Java 5+. In we declare the object that we’d like to mock. Notice that our AccountManager is an interface. The reason behind this is simple: the core EasyMock framework can mock only interface objects. In we call the createMock method to create a mock of the class that we want. In , as in listing 7.4, we create two account objects that we’re going to use in our tests. After that we start declaring our expectations. With EasyMock we declare the expectations in two ways. When the method return type is void, we call it on the mock object (as in ), or when the method returns any kind of object, then we need to use the expect and andReturn methods from the EasyMock API . Once we’ve finished defining the expectations, we need to call the replay method to announce it . In we call the transfer method to transfer some money between the two accounts, and in we assert the expected result. The @After method, which gets executed after every @Test method, holds the verification of the expectations. With EasyMock we can call the verify method with any mock object , to verify that the method-call expectations we declared were triggered.