Dashboard > Groovy > ... > Using Other Testing Frameworks > Using RMock with Groovy
Using RMock with Groovy Log In | Sign Up   View a printable version of the current page.

Added by Paul King , last edited by Tom Nichols on Sep 25, 2007  (view change)
Labels: 
(None)

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.

The Item Storer Example

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)
Site running on a free Atlassian Confluence Open Source Project License granted to The Codehaus. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.2 Build:#919 Nov 26, 2007) - Bug/feature request - Contact Administrators