gpt4 book ai didi

grails - 测试 grails Controller 抛出 java.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 19:35:08 25 4
gpt4 key购买 nike

下一个测试因 java.NullPointerException 而失败

package org.ttm

import grails.test.mixin.TestFor
import spock.lang.Specification

@TestFor(ActorController)
class ActorControllerSpec extends Specification {
StageDataService stageDataService = Mock()

void setup() {
controller.stageDataService = stageDataService
}

void 'index returns list of stages'() {
Long id = 20

when:
controller.index(id)

then:
1 * stageDataService.getActors(id)
}
}

这里有什么问题吗?

这是 StackTrace:

'2014-04-10 22:22:31,471 [main] ERROR StackTrace  - Full Stack Trace:
'java.lang.NullPointerException
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:128)
at org.codehaus.groovy.grails.orm.support.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:57)
at org.ttm.ActorControllerSpec.index returns list of stages(ActorControllerSpec.groovy:18)
'2014-04-10 22:22:31,472 [main] ERROR StackTrace - Full Stack Trace:
'java.lang.NullPointerException
at org.ttm.ActorControllerSpec.index returns list of stages(ActorControllerSpec.groovy:18)
'2014-04-10 22:22:31,473 [main] ERROR StackTrace - Full Stack Trace:
'java.lang.NullPointerException
at org.ttm.ActorControllerSpec.index returns list of stages(ActorControllerSpec.groovy:18)
'2014-04-10 22:22:31,473 [main] ERROR StackTrace - Full Stack Trace:
'java.lang.NullPointerException
at org.ttm.ActorControllerSpec.index returns list of stages(ActorControllerSpec.groovy:18)
'2014-04-10 22:22:31,474 [main] ERROR StackTrace - Full Stack Trace:
'java.lang.NullPointerException
at org.ttm.ActorControllerSpec.index returns list of stages(ActorControllerSpec.groovy:18)
'2014-04-10 22:22:31,474 [main] ERROR StackTrace - Full Stack Trace:
'java.lang.NullPointerException
at org.ttm.ActorControllerSpec.index returns list of stages(ActorControllerSpec.groovy:18)
'2014-04-10 22:22:45,664 [main] ERROR StackTrace - Full Stack Trace:

好的,我认为这可能是问题所在:http://jira.grails.org/browse/GRAILS-10538 .我的 Controller 装饰有 grails.transaction.Transactional 标签。解决方法是使用 @Mock 标签模拟一个类。 Holly Molly Bugs。

最佳答案

很难说没有堆栈跟踪或任何东西,但是 1 * StageDataService.getActors(id)绝对应该是 1 * stageDataService.getActors(id) .

关于grails - 测试 grails Controller 抛出 java.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23001045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com