RMock is a Java mock object framework typically used with JUnit 3.x. RMock has support for a setup-modify-run-verify workflow when writing JUnit tests. It integrates better with IDE refactoring support than some other popular mocking frameworks and allows designing classes and interfaces in a true test-first fashion.
The sections below illustrate using RMock for the mocking parts of Using Testing Frameworks with Groovy.
We are going to consider how you might use RMock as part of testing the Item Storer Example.
Here is how we can test JavaStorer:
// require(groupId:'com.agical.rmock', artifactId:'rmock', version='2.0.2')
// require(groupId:'junit', artifactId:'junit', version='3.8.2')
// require(groupId:'cglib', artifactId:'cglib-nodep', version='2.2_beta1')
import com.agical.rmock.extension.junit.RMockTestCase
class RmockTest extends RMockTestCase {
def mockReverser, storer
protected void setUp() throws Exception {
mockReverser = mock(Reverser.class, 'mockReverser')
storer = new JavaStorer(mockReverser)
}
void testStorage() {
expectReverse(123.456, -123.456)
expectReverse('hello', 'olleh')
startVerification()
checkReverse(123.456, -123.456)
checkReverse('hello', 'olleh')
}
def expectReverse(input, output) {
mockReverser.reverse(input)
modify().returnValue(output)
}
def checkReverse(value, reverseValue) {
storer.put(value)
assert value == storer.get()
assert reverseValue == storer.getReverse()
}
}
def suite = new junit.framework.TestSuite()
suite.addTestSuite(RmockTest.class)
junit.textui.TestRunner.run(suite)
|