2014-04-10 177 views
3

下一個測試與java.NullPointerException測試的Grails控制器拋出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) 
    } 
} 

這裏有什麼問題沒有?

這是堆棧跟蹤:

'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標記模擬一個類。霍莉莫莉蟲蟲。

回答

0

很難說沒有堆棧跟蹤或任何東西,但1 * StageDataService.getActors(id)應該肯定是1 * stageDataService.getActors(id)

+0

你打敗了我。 :) – dmahapatro

+0

是的,它是'stageDataService.getActors(id)'。我編輯了這個問題來放置StackTrace。 –