【发布时间】:2011-03-01 12:02:26
【问题描述】:
我想为我的 grails 应用程序编写单元测试。
但是groovy中null对象模式存在问题。
我使用 for 创建一个实例:
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。
我没有找到解决方案。我该如何解决这个问题?
【问题讨论】:
-
当性能为空时,我无法完全理解您还在等待什么其他值?
-
我期望 KeywordClickPerformance 提供一个字符串,但性能可能不为空
标签: unit-testing grails groovy nullpointerexception