grails unit test - java.lang.NullPointerException:无法在null对象上调用方法finish()

时间:2013-11-06 20:09:10

标签: unit-testing grails

我无法在grails中运行单元测试。我有一个TransactionController(也可以github获得)方法transactionAnalytics我想测试如下,

TransactionController.groovy

package eccount


import org.codehaus.groovy.grails.web.json.JSONObject
import org.springframework.dao.DataIntegrityViolationException
import grails.converters.JSON

class TransactionController {
   def transactionService

   def transactionAnalytics = {
       searchRequest = searchRequest ?: new SearchRequest(requestParams: new HashMap<String, String>())
       configureRequestParams()
       def responseBytes = transactionService.getSearchResponse(searchRequest)
       def jsonResponse
       if (responseBytes)
            jsonResponse = JSON.parse(responseBytes)
       else
           jsonResponse = new JSONObject()
       render jsonResponse as JSON
   }
 }

TransactionController#transactionAnalyticsalso available at github)的相应测试如下,

TransactionControllerTests.groovy

package eccount



import org.junit.*
import grails.test.mixin.*

@TestFor(TransactionController)
class TransactionControllerTests {
    def INDEX_NAME = "gccount"

    void testTransactionAnalytics(){
        Map<String, String> params = new HashMap<String, String>()
        params.put("indexName",INDEX_NAME)
        //println params.get("indexName")
        //controller.params  = params
        controller.params.indexName  = "gccount"
        controller.transactionAnalytics()
        def jsonResponse = controller.response.json
        println jsonResponse
    }
}

当我运行Controller的方法时,我得到以下异常

prayag@prayag:/backup/gccount$ grails test-app TransactionController.transactionAnalytics
| Environment set to test.....

| Running 1 unit test...
| Failure:  Test mechanism
|  java.lang.NullPointerException: Cannot invoke method finish() on null object
    at org.junit.runner.notification.RunNotifier$2.notifyListener(RunNotifier.java:71)
    at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:41)
    at org.junit.runner.notification.RunNotifier.fireTestRunFinished(RunNotifier.java:68)
    at _GrailsTest_groovy$_run_closure4.doCall(_GrailsTest_groovy:290)
    at _GrailsTest_groovy$_run_closure2.doCall(_GrailsTest_groovy:248)
    at _GrailsTest_groovy$_run_closure1_closure21.doCall(_GrailsTest_groovy:195)
    at _GrailsTest_groovy$_run_closure1.doCall(_GrailsTest_groovy:184)
    at TestApp$_run_closure1.doCall(TestApp.groovy:82)
| Completed 0 unit test, 1 failed in 2723ms

| Packaging Grails application.....
[scalaPlugin] Compiling Scala sources from plugins to /home/prayag/.grails/2.1.1/projects/cashless/plugin-classes
[scalaPlugin] Compiling Scala sources to /backup/gccount/target/classes
| Compiling 2 source files

Configuring Spring Security Core ...
... finished configuring Spring Security Core
sandbox user created
role created
stall created with a user
| Tests FAILED  - view reports in /backup/gccount/target/test-reports

同样,file:///backup/gccount/target/test-reports没有留下任何报告。 无论如何,谁在这里实际上是空的?

Unit tests

资源

9 Testing - Reference Documentation

http://mrhaki.blogspot.com/2010/04/grails-goodness-invoking-single-test.html

https://stackoverflow.com/a/3736549/432903

2 个答案:

答案 0 :(得分:1)

尝试:

grails test-app TransactionController.testTransactionAnalytics

你忘记了方法名称前面的“测试”... 是的......似乎,你不必在类名中写它,但在方法名中你必须...

答案 1 :(得分:0)

Grails Goodness: Invoking a Single Test Method似乎发布了错误信息,因为以下代码无效。

$ grails test-app TransactionController.transactionAnalytics

正确的是初始化transactionService

$ grails test-app TransactionControllerTests.testTransactionAnalytics

OR

$ grails test-app TransactionController.testTransactionAnalytics