After upgrading a Grails 2.5.1 application to Grails 3.0.6 I am having troubles to get my unit tests working again.
I have a very simple domain class called 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
}
}
and a unit test that does this
@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')
}
}
When I execute the unit test I get the following runtime error
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)
It complains about a dynamic finder that was available in Grails 2.5.x. Can anyone help me to solve that problem? Anyone had such a thing before?
EDIT
I created a plain new Grails 3.0.6 and generated a domain class by the task create-domain-class
. Then I added the three String attributes (name, shortName, image) to the class and wrote a simple assertion !Team.findByName('A')
but the exception remains.
EDIT 2 I still have no clue what I am doing wrong. I reported that issue to the Grails-Core issue tracker. Example code can be found here
EDIT 3
@TestFor
does the job but I would like to work with the HibernateTestMixin
instead of DomainClassUnitTestMixin
in favour of a H2 in memory database. Therefore I added
testCompile 'org.grails:grails-datastore-test-support:4.0.5.RELEASE'
to my build.gradle
. But when I add the HibernateTestMixin
I get the following exception.
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)
The behaviour is officially a bug that will be fixed by the release of Grails 3.0.8