测试grails控制器抛出java.NullPointerException

时间:2014-04-10 23:41:12

标签: grails groovy spock

下一个测试失败,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。我的控制器使用grails.transaction.Transactional标记进行修饰。解决方法是使用@Mock标记模拟一个类。 Holly Molly Bugs。

1 个答案:

答案 0 :(得分:0)

很难说没有堆栈跟踪或任何内容,但1 * StageDataService.getActors(id)肯定应该是1 * stageDataService.getActors(id)