简单Grails控制器单元测试中的Nullpointer [英] Nullpointer in simple grails controller unit test
问题描述
对于一个非常基本的Grails 2.4.1控制器的单元测试,我需要解决一个奇怪的问题.
I need a little assistance with a strange issue I am facing unit testing a very basic Grails 2.4.1 controller.
使用此控制器:
class AuthenticationEventController {
def index() {
// Sorry, ajax only!
if(!request.xhr) {
redirect(controller: "main")
return false
}
render(template: "index")
return
}
}
这个测试:
@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'
}
}
我在"controller.index()"调用上收到NullPointerException.
I get a NullPointerException on the "controller.index()" call.
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
代替
import org.springframework.transaction.annotation.Transactional
groovy类中的 . for 为什么没有关于主要区别的明确答案,或者为什么测试对此做得不好.
同样,通常只有在您要测试一个后面有2个更多类层的类时,才会发生这种情况. 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. 这篇关于简单Grails控制器单元测试中的Nullpointer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!@Transactional
annotation in the groovy class.