sme*_*eeb 3 java groovy unit-testing spock
这是我的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
}
Run Code Online (Sandbox Code Playgroud)
这是FizzBuzz模拟:
public interface FizzBuzz extends Serializable {
Map<String, Object> getAttributes();
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我得到:
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
Run Code Online (Sandbox Code Playgroud)
它在第18行引用的'null对象'是fizzBuzz其attributes映射或其映射.为什么?
您正在尝试使用多个级别的间接,并且>>正在应用于结果.attributes["favcolor"],该结果为null(因为它.attributes是一个空映射).相反,只需初始化地图:
fizzBuzz.attributes >> [favcolor: 'red', age: 36]
Run Code Online (Sandbox Code Playgroud)
(另外,你真的age想成为一个字符串吗?)
| 归档时间: |
|
| 查看次数: |
1703 次 |
| 最近记录: |