JDummy is a thin API which sits above JMock. It allows very succinct expectation setting code when the expectation code would normally involve many stubs.
The sections below illustrate using JDummy for the mocking parts of Using Testing Frameworks with Groovy.
We are going to consider how you might use JDummy as part of testing the Item Storer Example.
Here is how we can test JavaStorer:
// require(groupId:'jmock', artifactId:'jmock', version='1.2.0')
// require(groupId:'jmock', artifactId:'jmock-cglib', version='1.2.0')
// require(groupId:'junit', artifactId:'junit', version='3.8.2')
// require(groupId:'cglib', artifactId:'cglib-nodep', version='2.2_beta1')
// require(url:'jdummy.sf.net', jar:'jdummy-1.3.3.jar')
import net.sf.jdummy.JDummyTestCase
class JDummyTest extends JDummyTestCase {
def mockReverser, storer
protected void setUp() throws Exception {
mockReverser = mimicWithDummyValues(Reverser.class)
storer = new JavaStorer(mockReverser)
}
void testStorage() {
expectReverse(123.456, -123.456)
expectReverse('hello', 'olleh')
checkReverse(123.456, -123.456)
checkReverse('hello', 'olleh')
}
def expectReverse(input, output) {
// with is a keyword in Groovy so we quote it
assertBehavior(mockReverser).expects(once()).method('reverse').'with'(eq(input)).will(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(JDummyTest.class)
junit.textui.TestRunner.run(suite)
|
Note: Our example is so simple, that JDummy's power is not really illustrated here.