Unit testing grails测试中有趣的空指针异常
我想为我的grails应用程序编写单元测试 但是groovy中存在一个关于Unit testing grails测试中有趣的空指针异常,unit-testing,grails,groovy,nullpointerexception,Unit Testing,Grails,Groovy,Nullpointerexception,我想为我的grails应用程序编写单元测试 但是groovy中存在一个关于null对象模式的问题 我使用以下命令创建实例: KeywordClickPerformance performance = KeywordClickPerformance.forA("string").from(startDate).to(endDate) 但是像这样使用的时候, void testForA() { assertEquals "string", performance.keyword.name
null
对象模式的问题
我使用以下命令创建实例:
KeywordClickPerformance performance = KeywordClickPerformance.forA("string").from(startDate).to(endDate)
但是像这样使用的时候,
void testForA() {
assertEquals "string", performance.keyword.name
}
在执行单元测试之后,grails给出了以下异常:
"java.lang.NullPointerException: Cannot get property 'name' on null object at "
我为解决方案使用了performance?.keyword?.name
,但它返回null
,因为performance
是null
我没有找到解决办法。如何解决此问题?您需要在测试或设置中创建性能。否则,您的forA方法不正确…未创建性能。您需要在=右侧的KeyworkClickPerformance前面添加新的。我认为groovy将允许对构造函数进行链接。如果没有,可以分两行完成 关键字ClickPerformance=新关键字ClickPerformance()
performance.forA(“字符串”).from(startDate).to(endDate)当性能为空时,我无法完全理解您正在等待的其他值是什么?我希望关键字ClickPerformance中包含字符串,但性能可能不为空我们使用关键字ClickPerformance=KeywordClickPerformance.forA(“字符串”)创建性能。。。。在forA方法中,perormance为null,这已经是问题所在。好的,我尝试了这个,但没有正确的解决方案。谢谢。