Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing grails测试中有趣的空指针异常_Unit Testing_Grails_Groovy_Nullpointerexception - Fatal编程技术网

Unit testing grails测试中有趣的空指针异常

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

我想为我的grails应用程序编写单元测试

但是groovy中存在一个关于
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,这已经是问题所在。好的,我尝试了这个,但没有正确的解决方案。谢谢。