Ivan Alagenchev
Ivan Alagenchev

Reputation: 552

How to mock a java class inside a Grails unit test?

I have a java class, which I've added to my resources.groovy like this:

import persistentrep.ReasoningXMLLoader;
// Place your Spring DSL code here
beans = {
reasoningXMLLoader(ReasoningXMLLoader)
}

I then use this class in my GrailsService:

class XMLService
{
def reasoningXMLLoader

    def someMethod()
    {
        reasoningXMLLoader.doSomething()
    }
}

Obviously, I want to write a unit test that mocks reasoningXMLLoader and injects it into the service. I tried using @Mock, MockFor and mockFor and I keep getting org.codehaus.groovy.grails.exceptions.GrailsConfigurationException: Cannot add Domain class [class persistentrep.ReasoningXMLLoader]. It is not a Domain! error :

org.codehaus.groovy.grails.exceptions.GrailsConfigurationException: Cannot add Domain class [class persistentrep.ReasoningXMLLoader]. It is not a Domain!
at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.addArtefact(DefaultGrailsApplication.java:916)
at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.addArtefact(DefaultGrailsApplication.java:615)
at org.codehaus.groovy.grails.commons.GrailsApplication$addArtefact.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at grails.test.mixin.domain.DomainClassUnitTestMixin.mockDomain(DomainClassUnitTestMixin.groovy:131)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSite.invoke(PogoMetaMethodSite.java:226)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:52)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:46)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:145)
at grails.test.mixin.domain.DomainClassUnitTestMixin.mockDomain(DomainClassUnitTestMixin.groovy:128)
at com.vpundit.www.XMLServiceTests.mockDomain(XMLServiceTests.groovy)
at com.vpundit.www.XMLServiceTests.setUp(XMLServiceTests.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Is there a way to mock this using the Grails built-in mocking framework, or do I need to use something like Mockito?

What is the best way to inject the mock? Is there some Grails magic that can do it for me, or do I just use the exposed property - service.reasoningXMLLoader = mock?

Upvotes: 1

Views: 3518

Answers (1)

Ivan Alagenchev
Ivan Alagenchev

Reputation: 552

I finally managed to figure out how to do this. I was getting the exception, because I was trying to use the @Mock() attribute, which apparently cannot be used for what I was trying to do.

The task is actually pretty straight forward after reading the Groovy documentation for mocking with closures and with MockFor. I should have realized earlier that Groovy's documentation is better suited for this type of information that Grails'

So here is how to accomplish what I wanted to do, using closures. It's very neat actually:

void testParseXml() {
    String xml = "test string"
    def mockReturnObject = {create your object to be returned here}
    def loader = [parseString:{ mockReturnObject }] as ReasoningXMLLoader
    service.reasoningXMLLoader = loader
    def result = service.parseXML(xml)
    do assertions here
}

Here is how to do the same with mocks:

void testParseXml() {
    String xml = "test string"
    def mockReturnObject = {create your object to be returned here}
    def loader = new MockFor(ReasoningXMLLoader)
    loader.demand.parseString{mockReturnObject}
    service.reasoningXMLLoader = loader.proxyInstance()
    def result = service.parseXML(xml)
    do assertions here
}

I hope this helps someone who is new to Grails and Groovy.

Upvotes: 3

Related Questions