简体   繁体   中英

Grails 3.x - tests from grails 2.5.x don't work

I have a domain class, which extends non-domain class. When my project was on Grails 2.5.3, the test worked fine.

@Mock(Activity)
class ActivitySpec extends Specification {
    def "test"(){
        expect:
        new Activity(name: 'dfd').save()
    }
}

The domain

class Activity extends DomainRestResource {
    String name
    String code
    String description
    static hasMany = [....]
    static belongsTo = [... ]

    static constraints = {
        name maxSize: 50
       ....
    }

    static mapping = {
        table name: 'tt_activity'
    }
}

DomainRestResource is defined in src/main/groovy/com/...

DomainRestResource.groovy

abstract class DomainRestResource extends UniversalRestResource {

@Autowired
def connectionManager
@Autowired
def userActivityService
@Autowired
def dataSource

protected transient int limit
protected transient int offset
private transient String tableName

/*
many static methods and fields
and some logic
*/

}

And UniversalRestResource.groovy

abstract class UniversalRestResource {

    /*
    some logic
    */

    abstract List<Object> findObjectsByQuery(String query, int limit, int offset)
   /*and any others abstract methods*/
}

Now i'm upgrading the project to Grails 3.1.1 and everything works fine except all my tests. Test result on grails 3.1.1:

org.grails.datastore.mapping.model.IllegalMappingException: Mapped identifier [id] for class [com.astaprime.rest.DomainRestResource] is not a valid property

    at org.grails.datastore.mapping.model.config.GormMappingConfigurationStrategy.getIdentity(GormMappingConfigurationStrategy.java:887)
    at org.grails.datastore.mapping.model.AbstractPersistentEntity.resolveIdentifier(AbstractPersistentEntity.java:196)
    at org.grails.datastore.mapping.model.AbstractPersistentEntity.initialize(AbstractPersistentEntity.java:117)
    at org.grails.datastore.mapping.model.AbstractPersistentEntity.getRootEntity(AbstractPersistentEntity.java:221)
    at org.grails.datastore.mapping.model.AbstractMappingContext.initializePersistentEntity(AbstractMappingContext.java:271)
    at org.grails.datastore.mapping.model.AbstractMappingContext.initialize(AbstractMappingContext.java:250)
    at grails.test.mixin.domain.DomainClassUnitTestMixin.initializeMappingContext(DomainClassUnitTestMixin.groovy:93)
    at grails.test.mixin.domain.DomainClassUnitTestMixin.mockDomains(DomainClassUnitTestMixin.groovy:87)
    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.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

If remove "extends DomainRestResource" from domain, the test succeeds. I cannot remove it from all my domain classes, it's very important for program logic. Haw can i fix this? Thanx!

解决的方法是将Grails升级到版本3.1.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM