If you are familiar with unit testing and test driven development then you are most probably aware of Mock Objects.
The EasyMock website states that:
So how do you use them? Basically you want to test some code that depends on a number of other objects. Some of these objects might have unwanted side effects like talking to a database, or accessing an external URL.
Rather than passing in the real implementation (with the above side effects) you would pass in a mock or stub instead. The mock can be pre-wired to expect a number of calls to it's methods from the code under test and will record these calls for verification later. Typically these mock objects were written by hand and so have fairly simple implementations, EasyMock removes that hard work by using Java's proxy mechanism to do most of the heavy lifting for you.
EasyMock has four stages:
Let's assume we are developing a reporting object which manipulates the output of a database query. It's going to parse through a JDBC ResultSet object and do something with the results. We want the
Technorati Tags: Java, UnitTesting, TDD, JUnit, EasyMock, MockObjects, Steve Freeman, Tim Mackinnon, Philip Craig, AndrewBeacock
The EasyMock website states that:
A Mock Object is a test-oriented replacement for a collaborator. It is configured to simulate the object that it replaces in a simple way. In contrast to a stub, a Mock Object also verifies whether it is used as expected.Mock Objects were introduced at XP 2000 by Tim Mackinnon, Steve Freeman and Philip Craig with the Endo-Testing: Unit Testing with Mock Objects paper.
So how do you use them? Basically you want to test some code that depends on a number of other objects. Some of these objects might have unwanted side effects like talking to a database, or accessing an external URL.
Rather than passing in the real implementation (with the above side effects) you would pass in a mock or stub instead. The mock can be pre-wired to expect a number of calls to it's methods from the code under test and will record these calls for verification later. Typically these mock objects were written by hand and so have fairly simple implementations, EasyMock removes that hard work by using Java's proxy mechanism to do most of the heavy lifting for you.
EasyMock has four stages:
createMock
- Create the mock based on an existing interface or classexpect
- Record the method calls that you expect your code under test to call indicating what you expect to be returned, if validreplay
- Set the mock into 'playback' mode which switches the mock into 'real' mode and starts recording method calls and returning objects as set up in the previous stageverify
- make sure that the right methods were called in the right order
import static org.easymock.EasyMock.*;you use this instead:
import static org.easymock.classextension.EasyMock.*;And now for an example...
Let's assume we are developing a reporting object which manipulates the output of a database query. It's going to parse through a JDBC ResultSet object and do something with the results. We want the
parse()
method to return false if there are no rows within the ResultSet:class ResultReporter {So how do we test this method? We could run an SQL query against a database and then pass the ResultSet in but that would be slow and rely on external resources. We could handcode a mock version - but have you seen how many methods the ResultSet interface has?!? Or we could use EasyMock to help us out:
boolean parse(ResultSet results) throws SQLException {
if (!results.next()) {
return false;
}
// do something with the results
return true;
}
}
public class ResultReporterTest {
@Test
public void parseReturnsFalseIfPassedAnEmptyResultSet() throws SQLException {
ResultSet mockResultSet = createMock(ResultSet.class); // 1
expect(mockResultSet.next()).andReturn(false); // 2
replay(mockResultSet); // 3
ResultReporter reporter = new ResultReporter();
assertFalse(reporter.parse(mockResultSet));
verify(mockResultSet); // 4
}
}
Technorati Tags: Java, UnitTesting, TDD, JUnit, EasyMock, MockObjects, Steve Freeman, Tim Mackinnon, Philip Craig, AndrewBeacock
Comments