gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:14:04 24 4
gpt4 key购买 nike

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 个失败:集成测试失败:合并测试报告

我跑“test-app -integration MySpec”命令

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

更新!

我找到了一个解决方案 - 只需添加testCompile "org.grails.plugins:hibernate"构建.gradle

最佳答案

在使用 Spock 之前,测试从未如此简单和直观。我们始终可以在 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
}

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

关于任何集成测试中的 Grails 3.0.6 java.lang.IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32551577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com