[英]Nullpointer in simple grails controller unit test
I need a little assistance with a strange issue I am facing unit testing a very basic Grails 2.4.1 controller. 我需要一些奇怪的问题,我正面临单元测试一个非常基本的Grails 2.4.1控制器。
Given this controller: 鉴于此控制器:
class AuthenticationEventController {
def index() {
// Sorry, ajax only!
if(!request.xhr) {
redirect(controller: "main")
return false
}
render(template: "index")
return
}
}
And this test: 而这个测试:
@TestFor(AuthenticationEventController)
class AuthenticationEventControllerSpec extends Specification {
void "Test that the index rejects non-ajax calls"() {
given:
request.isXhr = { false }
when:
controller.index()
then:
response.redirectedUrl == '/main'
}
}
I get a NullPointerException on the "controller.index()" call. 我在“controller.index()”调用上得到一个NullPointerException。
java.lang.NullPointerException
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130)
at org.codehaus.groovy.grails.orm.support.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:85)
at au.com.intrinsicminds.advansys.controller.AuthenticationEventControllerSpec.Test that the index rejects non-ajax calls(AuthenticationEventControllerSpec.groovy:17)
the problem most likely is you are using 最有可能的问题是你正在使用
import grails.transaction.Transactional
instead of 代替
import org.springframework.transaction.annotation.Transactional
for @Transactional
annotation in the groovy class. 对于groovy类中的
@Transactional
注释。
why, no clear answer as to the major difference or why the test doesn't do well with this. 为什么,没有明确的答案,主要的区别或为什么测试不能很好地解决这个问题。 Also this usually only happens if you are testing a class with 2 more class layers behind it.
此外,通常只有在您测试一个后面还有2个类层的类时才会发生这种情况。
Do you use a domain class anywhere else in your code? 您是否在代码中的任何其他位置使用域类? I had the same problem (NPE raised by TransactionTemplate#execute) and the solution was to use @Mock to one of my entities, as per this jira issue: https://jira.grails.org/browse/GRAILS-11045
我遇到了同样的问题(由TransactionTemplate引发的NPE #exend),解决方案是将@Mock用于我的一个实体,根据这个jira问题: https : //jira.grails.org/browse/GRAILS-11045
The following will work with Grails 2.4.1. 以下内容适用于Grails 2.4.1。
A controller: 控制器:
// grails-app/controllers/demo/AuthenticationEventController.groovy
package demo
class AuthenticationEventController {
def index() {
if(!request.xhr) {
redirect(controller: "main")
} else {
render(template: "index")
}
}
}
A unit test: 单元测试:
// test/unit/demo/AuthenticationEventControllerSpec.groovy
package demo
import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(AuthenticationEventController)
class AuthenticationEventControllerSpec extends Specification {
void "Test that the index redirects for non-ajax calls"() {
when:
controller.index()
then:
response.redirectedUrl == '/main'
}
void "Test that index renders template for ajax calls"() {
given:
request.makeAjaxRequest()
views['/authenticationEvent/_index.gsp'] = 'my template text'
when:
controller.index()
then:
response.contentAsString == 'my template text'
}
}
I hope that helps. 我希望有所帮助。
I got same stacktrace when tried to Spy Transactional service. 在尝试Spy Transactional服务时,我得到了相同的堆栈跟踪。
I found solution that helps me. 我找到了帮助我的解决方案。 So I just init transactionManager in spied service.
所以我只是在spied服务中初始化transactionManager。
See example: 见例子:
SomeTransactionalService sts = Spy(SomeTransactionalService)
sts.transactionManager = transactionManager // so you need to add init transactionManager
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.