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
}
这就是我们测试无效输入是否抛出异常的方法。 希望对你有帮助




