Plugins You can find out about all the publicly available Grails plugins.

JMock Support Plugin for Grails

  • Tags: /
  • Latest: 0.2
  • Last Updated: 02 July 2010
  • Grails version: 1.3.0 > *
  • Authors: null
0 vote
compile "org.grails.plugins:jmock-support:0.2"





Using the command line, step into the directory of your Grails project.

Install the plugin by entering the following:

grails install-plugin jmock-support


Grails JMock Support Plugin


A plugin for the Grails Framework that provides support for the JMock Mocking Framework. It adds JMock 2 functionality which integrates well with the new JUnit 4 upgrade that was added to Grails since 1.3.0.

Using It

Once the plugin is installed, you are ready to start using it. This plugin strives to give you all the functionality of existing Grails TestCases, with added functionality of having a Mockery instance as part of the test fixture. More specifically, it adds an instance of the JUnit4GroovyMockery to each test instance.

See this article for more details of the JUnit4GroovyMockery:

The Caveat

Beacause of Groovy's dynamic nature, the usual org.jmock.lib.legacy.ClassImposteriser does not work when set on the Mockery. It causes a big ugly stack overflow!

As a result, it is neccessary to define an interface for the class being mocked. I found it quit neat and even helpful to define the interface in the same file as the implementation. This makes your class readable and makes the contract of the class rather clear. See the Service example below.

A JUnit 3 Style Example

Writing a simple JUnit 3 style test case is as easy as extending the following TestCases instead of the old implementation:


  • grails.test.ControllerUnitTestCase with grails.test.jmock.JMockControllerUnitTestCase
  • grails.test.GrailsUnitTestCase with grails.test.jmock.JMockGrailsUnitTestCase
  • grails.test.MvcUnitTestCase with grails.test.jmock.JMockMvcUnitTestCase
  • grails.test.TagLibUnitTestCase with grails.test.jmock.JMockTagLibUnitTestCase
Here is a simple example of using it in a Test Case:
class MyControllerTests extends JMockControllerUnitTestCase {
    def myService

protected void setUp() { super.setUp() myService = mockery.mock(MyServiceIface) controller.myService = myService }

void testAction() { def result = 'The acid test!' = '100' mockery.checking { one(myService).tryit( will(returnValue(result)) }



assert renderArgs.view == 'success' assert renderArgs.model.result == result

} }

The Controller Code:

class MyController {

def myService

def action = { String result = myService.tryit( render(view:'success', model:[result:result]) } }

Finally, the Service being mocked. Notice the neccessity of having an interface defined!

interface MyServiceIface {
    String tryit(String id)

class MyService implements MyServiceIface{

static transactional = true

String tryit(String id) { "Hello World!: ${id}" } }

A JUnit 4 Style Example

class MyControllerJunit4Tests {
	def mockery
	def service
	def controller

@Before void setUp(){ mockery = new JUnit4GroovyMockery() service = mockery.mock(MyServiceIface) controller = new MyController() controller.myService = service controller.metaClass.params = [:] controller.metaClass.render = { assert it.view == 'success' assert it.model.result == 'The result' } }

@Test void testAction(){ def result = 'The result' controller.params.put 'id', '100' mockery.checking { one(service).tryit('100') will(returnValue(result)) }


mockery.assertIsSatisfied() } }

Added bonus: Power Asserts!

As of Version 0.2 of this plugin, I have added overrides for many of the most-used assertEquals methods provided by JUnit. Here is a list of all those supported:

static void assertEquals(Object expected, Object actual)
static void assertEquals(int expected, int actual)
static void assertEquals(long expected, long actual)
static void assertEquals(String expected, String actual)
static void assertNotSame(Object unexpected, Object actual)
static void assertSame(Object expected, Object actual)

Hope you enjoy using JMock with Grails!