上下文:我创建了一个名为“samplePlugin”的新 Grails 插件。它有一个域类 Person 和一个集成测试 PersonSpec。现在,当我运行时grails test-app
,测试失败并出现以下异常:
java.lang.IllegalStateException: Method on class [sampleplugin.Person] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.
at org.grails.datastore.gorm.GormEntity$Trait$Helper.currentGormInstanceApi(GormEntity.groovy:52)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.save(GormEntity.groovy:165)
at sampleplugin.PersonSpec.$tt__$spock_feature_0_1(PersonSpec.groovy:25)
at sampleplugin.PersonSpec.test save_closure2(PersonSpec.groovy)
at groovy.lang.Closure.call(Closure.java:423)
at groovy.lang.Closure.call(Closure.java:439)
at grails.transaction.GrailsTransactionTemplate$1.doInTransaction(GrailsTransactionTemplate.groovy:67)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.executeAndRollback(GrailsTransactionTemplate.groovy:64)
at sampleplugin.PersonSpec.test save(PersonSpec.groovy)
Person.groovy 类是:
package sampleplugin
class Person {
String name
}
PersonSpec.groovy 类是:
package sampleplugin
import grails.test.mixin.integration.Integration
import grails.transaction.*
import spock.lang.*
@Integration
@Rollback
class PersonSpec extends Specification {
void "canary test"() {
expect:
true == true
}
void "test save"() {
when:
new Person(name: "John Doe").save flush:true
then:
1 == Person.count()
}
}
这是示例 grails3 插件的 GitHub 链接https://github.com/puneetbehl/samplePlugin.git