Spock framework only allowed one exclusion condition per "then" block
org.spockframework:spock-core:1.0-groovy-2.4
Gradle 2.4
Groovy 2.3.10
I am using gradle and spock framework to test my java code. However, the function I'm testing may throw 3 different exceptions that I need to check. However, in my proposal, then
I list 3 exceptions and if any of them are not passed, the test will pass. However, I keep getting the following compilation error:
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
My spock function which I am using for testing. The function under test can exclude these exceptions.
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
}
Is there a way to check for more than 3 exceptions in one sentence then
?
I also tried this, which didn't work, splitting into 3 separate suggestions.
then:
notThrown XMPPErrorException
then:
notThrown NoResponseException
then:
notThrown NotConnectedException
source to share
the best i came up with is a suggestion 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]
}
I created a simpler example on spock web console
source to share
Unfortunately, it is impossible to test several operators notThrown
in one (or even several - chained) - then
blocks. As @BillJames pointed out, you can use noExceptionThrown
either the solution (just curiosity - I don't find it useful or readable). I prepared:
@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 {}
source to share