简体   繁体   中英

grails unit test - java.lang.NullPointerException: Cannot invoke method finish() on null object

I am not being able to run unit test in grails. I have a TransactionController (available at github as well) with method transactionAnalytics which I want to test as below,

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
   }
 }

Corresponding Tests for TransactionController#transactionAnalytics ( also available at github )is as below,

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
    }
}

When I run the method of the Controller, I get following exception

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

Again, there's no reports left at file:///backup/gccount/target/test-reports . Anyways, who is actually null here?

单元测试

Resources

9 Testing - Reference Documentation

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

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

try:

grails test-app TransactionController.testTransactionAnalytics

you forgot the "test" in front of the method name... and yes... it seems, you don't have to write it in the classname, but in the methodname you have to...

Grails Goodness: Invoking a Single Test Method seems posting wrong information because following code wouldn't work.

$ grails test-app TransactionController.transactionAnalytics

The correct one would be with initialized transactionService

$ grails test-app TransactionControllerTests.testTransactionAnalytics

OR

$ grails test-app TransactionController.testTransactionAnalytics

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM