Spock:在一个功能中测试下降和成功调用?

时间:2016-11-15 21:35:31

标签: unit-testing groovy spock

鉴于我想用两个参数测试我的方法:一个应该导致成功,另一个应该导致异常。

写一些像......

when:
myObject.myMethod(value);

then:
result

where:
value      | result
new Good() | notThrown(Exception)
new Bad()  | thrown(Exception)

..在Spock中是不可能的。

如何修复它并避免编写两个单独的功能方法?

1 个答案:

答案 0 :(得分:0)

import spock.lang.*

class MyFirstSpec extends Specification {

    def "let's try this!"() {
        when:
        new Test().method('Good')

        then:
        noExceptionThrown()

        when:
        new Test().method('Bad')

        then:
        thrown(Exception)
    }

    class Test {
        def method(String param) {
            if(param == 'Good') {
                return param
            }

            throw new Exception()
        }
    }
}

测试here