I have 2 test methods .
They all execute each line of the where block, I need a cleanup for add & relax methods.
I've tried cleanup block , void cleanup() , def cleanupSpec() , non suits .
How can I explicitly run a cleanup after specific method which have "where:" block?
def "Add"() {
setup :
expect :
where:
}
def "Relax"() {
setup :
expect :
where:
}
You can have a cleanup block in your method like so:
@Unroll
def "a method that tests stuff"(){
given:
def foo = fooDAO.save(new Foo(name: name))
when:
def returned = fooDAO.get(foo.id)
then:
returned.properties == foo.properties
cleanup:
fooDAO.delete(foo.id)
where:
name << ['one', 'two']
}
The "cleanup" block will get run once per test iteration.
If you use @Unroll
, then cleanup:
will be called for every entry in the where:
block. To only run cleanup once then move your code inside the def cleanupSpec()
closure.
@Shared
def arrayOfIds = []
@Unroll
def "a method that tests stuff"(){
given:
def foo = fooDAO.save(new Foo(name: name))
when:
def returned = fooDAO.get(foo.id)
arrayOfIds << foo.id
then:
returned.properties == foo.properties
where:
name << ['one', 'two']
}
def cleanupSpec() {
arrayOfIds.each {
fooDAO.delete(it)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With