Spock 框架每个 'then' 块只允许一个异常条件
Spock framework only one exception condition is allowed per 'then' block
org.spockframework:spock-core:1.0-groovy-2.4
Gradle 2.4
Groovy 2.3.10
我正在使用 gradle 和 spock 框架来测试我的 java 代码。但是,我正在测试的函数可以抛出 3 个我需要测试的不同异常。但是,在我的 then
子句中,我列出了 3 个异常,如果没有抛出其中任何一个,则测试将通过。但是,我不断收到以下编译错误:
318: Only one exception condition is allowed per 'then' block @ line 318, column 9.
notThrown NoResponseException
319: Only one exception condition is allowed per 'then' block @ line 319, column 9.
notThrown NotConnectedException
我用于测试的 spock 函数。被测试的函数可以抛出这些异常。
def 'Create instant pubsub node'() {
setup:
smackClient.initializeConnection(domain, serviceName, resource, port, timeout, debugMode)
smackClient.connectAndLogin(username, password)
when:
smackClient.getSPubSubInstance(smackClient.getClientConnection()).createInstantnode()
then:
notThrown XMPPErrorException
notThrown NoResponseException
notThrown NotConnectedException
}
有没有办法在单个 then
子句中测试 3 个以上的异常?
我也试过这个方法,但也没有用,因为它分为 3 个单独的 then 从句。
then:
notThrown XMPPErrorException
then:
notThrown NoResponseException
then:
notThrown NotConnectedException
不幸的是,无法检查单个(甚至多个 - 当链接时)- then
块中的多个 notThrown
语句。正如@BillJames 评论的那样,您可以使用 noExceptionThrown
或解决方案(只是出于好奇——我觉得它没有用,也没有可读性)我准备了:
@Grab('org.spockframework:spock-core:1.0-groovy-2.4')
@Grab('cglib:cglib-nodep:3.1')
import spock.lang.*
class Test extends Specification {
def sc = new SomeClass()
def 'spec 1'() {
when:
sc.someMethod()
then:
noExceptionThrown()
}
def 'spec 2'() {
when:
sc.someMethod()
then:
notThrown(e)
where:
e << [E1, E2, E3]
}
}
class SomeClass {
def someMethod() {
}
}
class E1 extends RuntimeException {}
class E2 extends RuntimeException {}
class E3 extends RuntimeException {}
我想出的最好办法是使用 where:
子句
def 'Create instant pubsub node'() {
setup:
smackClient.initializeConnection(domain, serviceName, resource, port, timeout, debugMode)
smackClient.connectAndLogin(username, password)
when:
smackClient.getSPubSubInstance(smackClient.getClientConnection()).createInstantnode()
then:
notThrown exception
where:
exception << [XMPPErrorException, NoResponseException, NotConnectedException]
}
我创建了一个simpler example on the spock web console
org.spockframework:spock-core:1.0-groovy-2.4
Gradle 2.4
Groovy 2.3.10
我正在使用 gradle 和 spock 框架来测试我的 java 代码。但是,我正在测试的函数可以抛出 3 个我需要测试的不同异常。但是,在我的 then
子句中,我列出了 3 个异常,如果没有抛出其中任何一个,则测试将通过。但是,我不断收到以下编译错误:
318: Only one exception condition is allowed per 'then' block @ line 318, column 9.
notThrown NoResponseException
319: Only one exception condition is allowed per 'then' block @ line 319, column 9.
notThrown NotConnectedException
我用于测试的 spock 函数。被测试的函数可以抛出这些异常。
def 'Create instant pubsub node'() {
setup:
smackClient.initializeConnection(domain, serviceName, resource, port, timeout, debugMode)
smackClient.connectAndLogin(username, password)
when:
smackClient.getSPubSubInstance(smackClient.getClientConnection()).createInstantnode()
then:
notThrown XMPPErrorException
notThrown NoResponseException
notThrown NotConnectedException
}
有没有办法在单个 then
子句中测试 3 个以上的异常?
我也试过这个方法,但也没有用,因为它分为 3 个单独的 then 从句。
then:
notThrown XMPPErrorException
then:
notThrown NoResponseException
then:
notThrown NotConnectedException
不幸的是,无法检查单个(甚至多个 - 当链接时)- then
块中的多个 notThrown
语句。正如@BillJames 评论的那样,您可以使用 noExceptionThrown
或解决方案(只是出于好奇——我觉得它没有用,也没有可读性)我准备了:
@Grab('org.spockframework:spock-core:1.0-groovy-2.4')
@Grab('cglib:cglib-nodep:3.1')
import spock.lang.*
class Test extends Specification {
def sc = new SomeClass()
def 'spec 1'() {
when:
sc.someMethod()
then:
noExceptionThrown()
}
def 'spec 2'() {
when:
sc.someMethod()
then:
notThrown(e)
where:
e << [E1, E2, E3]
}
}
class SomeClass {
def someMethod() {
}
}
class E1 extends RuntimeException {}
class E2 extends RuntimeException {}
class E3 extends RuntimeException {}
我想出的最好办法是使用 where:
子句
def 'Create instant pubsub node'() {
setup:
smackClient.initializeConnection(domain, serviceName, resource, port, timeout, debugMode)
smackClient.connectAndLogin(username, password)
when:
smackClient.getSPubSubInstance(smackClient.getClientConnection()).createInstantnode()
then:
notThrown exception
where:
exception << [XMPPErrorException, NoResponseException, NotConnectedException]
}
我创建了一个simpler example on the spock web console