grails - 由于grails-3.0.1插件应用程序中的IllegalStateException,因此无法运行集成测试

标签 grails grails-plugin grails-3.0 integration-testing

上下文:我创建了一个新的Grails插件,名为“samplePlugin”。它具有一个域类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插件https://github.com/puneetbehl/samplePlugin.git的GitHub链接

最佳答案

我可以通过在build.gradle中的依赖项下添加compile "org.grails.plugins:hibernate"来解决此问题。似乎Grails插件应用程序默认没有它。以下是build.gradle文件:

buildscript {
    ext {
        grailsVersion = project.grailsVersion
    }
    repositories {
        mavenLocal()
        maven { url "https://repo.grails.org/grails/core" }
    }
    dependencies {
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
    }
}

plugins {
    id "io.spring.dependency-management" version "0.4.0.RELEASE"
    id "com.jfrog.bintray" version "1.1"
}

version "0.1-SNAPSHOT"
group "org.grails.plugins"

apply plugin: 'maven-publish'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "spring-boot"
apply plugin: "org.grails.grails-plugin"
apply plugin: "org.grails.grails-gsp"
// Used for publishing to central repository, remove if not needed
apply from:'https://raw.githubusercontent.com/grails/grails-profile-repository/master/profiles/plugin/templates/grailsCentralPublishing.gradle'
apply from:'https://raw.githubusercontent.com/grails/grails-profile-repository/master/profiles/plugin/templates/bintrayPublishing.gradle'

ext {
    grailsVersion = project.grailsVersion
    gradleWrapperVersion = project.gradleWrapperVersion
}

sourceCompatibility = 1.7
targetCompatibility = 1.7


repositories {
    mavenLocal()
    mavenCentral()
    maven { url "https://repo.grails.org/grails/core" }
}

dependencyManagement {
    imports {
        mavenBom "org.grails:grails-bom:$grailsVersion"
    }
    applyMavenExclusions false
}

dependencies {
    provided 'org.springframework.boot:spring-boot-starter-logging'
    provided "org.springframework.boot:spring-boot-starter-actuator"
    provided "org.springframework.boot:spring-boot-autoconfigure"
    provided "org.springframework.boot:spring-boot-starter-tomcat"

    provided "org.grails:grails-web-boot"
    provided "org.grails:grails-dependencies"
    provided 'javax.servlet:javax.servlet-api:3.1.0'

    compile "org.grails.plugins:hibernate"

    testCompile "org.grails:grails-plugin-testing"

    console "org.grails:grails-console"
}

task wrapper(type: Wrapper) {
    gradleVersion = gradleWrapperVersion
}

关于grails - 由于grails-3.0.1插件应用程序中的IllegalStateException,因此无法运行集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29869765/

相关文章:

grails - Grails可搜索插件

grails - 在 Grails 应用程序之外使用 grails-mail-plugin

grails - 有没有办法用 mongodb 控制 gorm 中的加载关系?

grails - GORM : how to deal with static GORM methods 中的抽象域类

database - Grails 一直在删除我的表

grails - Gradle插件不适用于Grails 2.2.3

spring - 在普通Spring Service Bean上使用Grails缓存插件

grails - 解压缩deploy war文件后,grains 3.0.3 with asset-pipeline-gradle:2.3.8无法正常工作

unit-testing - 无法在 grails 单元测试中注入(inject)服务类 - 测试失败

oracle - 在 Grails 2.0.1 中使用 Oracle 10g CLOB