【发布时间】:2016-04-17 02:24:13
【问题描述】:
这是我的 Spock 单元测试:
def "when favorite color is red then doSomething produces empty list of things"() {
given:
FizzBuzz fizzBuzz = Mock(FizzBuzz)
fizzBuzz.name >> 'Dark Helmet'
fizzBuzz.attributes >> [:]
fizzBuzz.attributes["favcolor"] >> 'red'
fizzBuzz.attributes["age"] >> '36'
Something something = new Something(fizzBuzz)
when:
Whoah whoah = something.doSomething()
then:
!whoah.things
}
这是FizzBuzz 模拟:
public interface FizzBuzz extends Serializable {
Map<String, Object> getAttributes();
}
当我运行它时,我得到:
java.lang.NullPointerException: Cannot invoke method rightShift() on null object
at com.commercehub.houston.SomethingSpec.when favorite color is red then doSomething produces empty list of things fail(SomethingSpec.groovy:18)
Process finished with exit code 255
它在第 18 行引用的“空对象”是 fizzBuzz 或其 attributes 映射。 为什么?
【问题讨论】:
标签: java unit-testing groovy spock