升级到Grails 3.0.6后使用动态查找器时,使用HibernateTestMixin进行单元测试时出现MissingMethodException

时间:2015-09-13 12:39:19

标签: unit-testing grails gorm grails-3.0

将Grails 2.5.1应用程序升级到Grails 3.0.6后,我的单元测试再次运行时遇到了麻烦。

我有一个非常简单的域类Team

class Team {

   String name
   String shortName
   String image = 'noimage.jpg'

   static constraints = {
       name nullable: false, maxSize: 30, unique: true
       shortName nullable: true, maxSize: 5
       image nullable: false, maxSize: 30
   }
}

和执行此操作的单元测试

@TestMixin(HibernateTestMixin)
@Domain([Team])
class TeamSpec extends Specification {

    def setup() {
        new Team(name: 'Hello', shortName: 'Big').save()
    }

    def cleanup() {
    }

    void "test something"() {
        expect:"fix me"
            !Team.findByName('hello')
    }
}

当我执行单元测试时,我得到以下运行时错误

groovy.lang.MissingMethodException: No signature of method: static abc.Team.findByName() is applicable for argument types: (java.lang.String) values: [hello]
at abc.TeamSpec.test something(TeamSpec.groovy:24)

它抱怨Grails 2.5.x中提供的动态查找程序。任何人都可以帮我解决这个问题吗?以前有人有这样的事吗?

修改

我创建了一个普通的新Grails 3.0.6并通过任务create-domain-class生成了一个域类。然后我将三个String属性(name,shortName,image)添加到类中并编写了一个简单的断言!Team.findByName('A'),但异常仍然存在。

编辑2 我仍然不知道我做错了什么。我向Grails-Core issue tracker报告了这个问题。可以找到示例代码here

编辑3 @TestFor完成了这项工作,但我希望使用HibernateTestMixin而不是DomainClassUnitTestMixin来支持内存数据库中的H2。因此我添加了

testCompile 'org.grails:grails-datastore-test-support:4.0.5.RELEASE'

到我的build.gradle。但是当我添加HibernateTestMixin时,我得到以下异常。

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.grails.datastore.mapping.simple.SimpleMapDatastore] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:371)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968)
at grails.test.mixin.domain.DomainClassUnitTestMixin.getSimpleDatastore(DomainClassUnitTestMixin.groovy:76)
at grails.test.mixin.domain.DomainClassUnitTestMixin.mockDomains(DomainClassUnitTestMixin.groovy:86)
at grails.test.mixin.domain.DomainClassUnitTestMixin.mockDomain(DomainClassUnitTestMixin.groovy:119)
at grails.test.mixin.domain.DomainClassUnitTestMixin.mockDomain(DomainClassUnitTestMixin.groovy:118)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:153)
at org.spockframework.runtime.model.MethodInfo.invoke(MethodInfo.java:84)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:88)
at org.spockframework.runtime.extension.builtin.AbstractRuleInterceptor$1.evaluate(AbstractRuleInterceptor.java:37)
at grails.test.runtime.TestRuntimeJunitAdapter$1$2.evaluate(TestRuntimeJunitAdapter.groovy:46)
at org.spockframework.runtime.extension.builtin.TestRuleInterceptor.intercept(TestRuleInterceptor.java:38)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:88)
at org.spockframework.runtime.extension.builtin.AbstractRuleInterceptor$1.evaluate(AbstractRuleInterceptor.java:37)
at grails.test.runtime.TestRuntimeJunitAdapter$3$4.evaluate(TestRuntimeJunitAdapter.groovy:73)
at org.spockframework.runtime.extension.builtin.ClassRuleInterceptor.intercept(ClassRuleInterceptor.java:38)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:2)

它应该工作正常 - 这个例子使用grails 3.0.1。 http://grails.github.io/grails-doc/3.0.1/guide/testing.html#unitTestingDomains

我猜测使用@TestFor注释可能会为你做到这一点。它将根据您正在测试的内容设置所需的任何mixins。这是我的工作示例,它位于

/src/test/groovy/grails3test/TeamSpec.groovy:

package grails3test

import grails.test.mixin.TestFor
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Team)
class TeamSpec extends Specification {

    def setup() {
        Team t = new Team(name: 'alphabravo')
        t.save()
    }

    def cleanup() {
    }

    void "test something"() {
        expect:
        Team.findByName('alphabravo') != null
    }
}

答案 1 :(得分:0)

该行为正式成为一个错误,将由Grails 3.0.8发布修复

Link to the bug