简体   繁体   English

grails单元测试-java.lang.NullPointerException:无法在空对象上调用方法finish()

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

I am not being able to run unit test in grails. 我无法在grails中运行单元测试。 I have a TransactionController (available at github as well) with method transactionAnalytics which I want to test as below, 我有一个TransactionController (也可以在github上找到 ),它带有方法transactionAnalytics ,我想按以下方法进行测试,

TransactionController.groovy 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, TransactionController#transactionAnalytics相应测试( 也可以在github上找到 )如下所示,

TransactionControllerTests.groovy 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 当我运行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

Again, there's no reports left at file:///backup/gccount/target/test-reports . 同样, file:///backup/gccount/target/test-reports上没有任何file:///backup/gccount/target/test-reports Anyways, who is actually null here? 无论如何,这里实际上谁是空的?

单元测试

Resources 资源

9 Testing - Reference Documentation 9测试-参考文档

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

https://stackoverflow.com/a/3736549/432903 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的优点:调用单个测试方法似乎发布了错误的信息,因为遵循以下代码将无法正常工作。

$ grails test-app TransactionController.transactionAnalytics

The correct one would be with initialized transactionService 正确的方法是使用初始化的transactionService

$ grails test-app TransactionControllerTests.testTransactionAnalytics

OR 要么

$ grails test-app TransactionController.testTransactionAnalytics

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NullPointerException:无法在空对象上调用方法queryForList() - java.lang.NullPointerException: Cannot invoke method queryForList() on null object java.lang.NullPointerException:无法调用“com.proj.my.repository.OrderRepository.save(Object)”,因为“this.orderRepository”是 null - java.lang.NullPointerException: Cannot invoke "com.proj.my.repository.OrderRepository.save(Object)" because "this.orderRepository" is null TestNG:@Test方法中的java.lang.NullPointerException - TestNG: java.lang.NullPointerException in the @Test Method 单元测试。 java.lang.NullPointerException - Unit testing. java.lang.NullPointerException 在使用render进行控制器方法的单元测试期间,Grails无法在null对象上调用getTimeZone() - Grails cannot invoke getTimeZone() on null object during unit testing of method of controller with render JUnit测试用例:java.lang.NullPointerException - JUnit Test case : java.lang.NullPointerException JUnit 测试用例:java.lang.NullPointerException - JUnit test case: java.lang.NullPointerException Grails-单元测试中的“无法在空对象上获取属性&#39;grails&#39;” - Grails - “Cannot get property 'grails' on null object” from unit test 对象实例化时的java.lang.NullPointerException - java.lang.NullPointerException at object instantiation 单元测试spring服务类中的java.lang.NullPointerException - java.lang.NullPointerException in Unit Testing spring service class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM