1

我有一个 java 类,我已将其添加到我的 resources.groovy 中,如下所示:

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

然后我在我的 GrailsS​​ervice 中使用这个类:

class XMLService
{
def reasoningXMLLoader

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

显然,我想编写一个模拟推理XMLLoader 并将其注入服务的单元测试。我尝试使用 @Mock、MockFor 和 mockFor,但我不断收到org.codehaus.groovy.grails.exceptions.GrailsConfigurationException:无法添加域类 [class persistentrep.ReasoningXMLLoader]。它不是域!错误 :

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)

有没有办法使用 Grails 内置的模拟框架来模拟它,还是我需要使用 Mockito 之类的东西?

注入模拟的最佳方法是什么?是否有一些 Grails 魔法可以为我做到这一点,或者我只是使用暴露的属性 - service.reasoningXMLLoader = mock?

4

1 回答 1

3

我终于设法弄清楚如何做到这一点。我遇到了异常,因为我试图使用 @Mock() 属性,这显然不能用于我想做的事情。

在阅读了有关使用闭包MockFor进行模拟的 Groovy 文档之后,该任务实际上非常简单。我早该意识到 Groovy 的文档更适合 Grails 的此类信息

所以这里是如何使用闭包来完成我想做的事情。实际上非常整洁:

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
}

以下是如何对模拟执行相同操作:

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
}

我希望这对刚接触 Grails 和 Groovy 的人有所帮助。

于 2012-08-25T18:16:58.857 回答