Grails 3.0.6 java.lang.IllegalStateException 任何集成测试

Grails 3.0.6 java.lang.IllegalStateException on any integration test

import grails.test.mixin.integration.Integration
import spock.lang.Specification

@Integration
class MySpec extends Specification {

   def setup() {
   }

   def cleanup() {
   }

    void "test something"() {
        expect:"tests a"
          true
    }
}

MySpec > test something FAILED
java.lang.IllegalStateException
    Caused by: org.springframework.beans.factory.BeanCreationException
        Caused by: org.springframework.beans.factory.BeanCreationException
            Caused by: org.springframework.beans.factory.BeanCreationException
                Caused by: java.lang.NullPointerException

1 项测试已完成,1 项失败 :集成测试失败 :mergeTestReports

我运行 "test-app -integration MySpec" 命令

这是 grails 的错误还是我做错了?

更新!

我找到了解决方案 - 只需添加 测试编译 "org.grails.plugins:hibernate" 至 build.gradle

在使用 Spock.We 始终可以在 grails Spock 测试中测试异常之前,测试从未如此简单和直观。例如,我们有一个抛出异常的方法:

String getUserType(int age){
    if(age<=0){
        throw new MyException("Invalid age")
    }else if( age>0 && age<50){
        return "Young"
    }else{
        return "Old"
    }
}

现在我们将编写此方法的测试用例来检查是否会因无效输入而抛出异常。

def "exception should be thrown only for age less than or equal to 0"{
    given:
        String type = getUserType(34)
    expect:
        type == "Young"
        notThrown MyException
    when:
        type = getUserType(0)
    then:
        thrown MyException
}

这就是我们测试无效输入是否抛出异常的方法。 希望对你有帮助