gpt4 book ai didi

Grails 集成测试 - getBean() 的空对象

转载 作者:行者123 更新时间:2023-11-28 20:20:32 27 4
gpt4 key购买 nike

我不明白为什么我的集成测试会抛出异常。

集成测试##

package sample

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

/**
* See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
*/
@TestFor(UserController)
class UserControllerTests extends GroovyTestCase {

User user
UserController uc

void setUp() {
//Save a User
user = new User(userName: "User1", firstName: "User1FN", lastName: "User1LN")
user.save()

//Set up UserController
uc = new UserController()
}

void tearDown() {
user.delete()
}

/**
* Test the UserController.handleLogin action.
*
* If the login succeeds, it will put the user object into the session.
*/

void testHandleLogin() {
//Setup controller paramaters
uc.params.userName = user.userName

//Call the action
uc.handleLogin()

//if the action functioned correctly, it put a user object into the session
def sessUser = uc.session.user
assert sessUser
assertEquals("Expected ids to match", user.id, sessUser.id)
//And the user was redirected to the Todo Page
assertTrue uc.response.redirectedUrl.startsWith("/todo")
}

/**
* Test the UserController.handleLogin action.
*
* If the login fails, it will redirect to login and set a flash message.
*
*/

void testHandleLoginInvalidUser() {
//Setup controller parameters
uc.params.userName = "Invalid_Name"

//Call the action
uc.handleLogin()
assertEquals "/user/login", uc.response.redirectedUrl
def message = uc.flash.message
assert message
assert message.startsWith("User not found")
}

/*
* Test the UserController.logout action
*
* If the logout action succeeds, it will remove the user object from the session.
*/
void testLogout (){
//Make a user logged into session
uc.session.user = user


//Call the action
uc.logout()
def sessUser = uc.session.user
assertNull ("Expected session user to be null", sessUser)
assertEquals "/user/login", uc.response.redirectedUrl
}

}

测试控制台输出

| Loading Grails 2.2.3
| Configuring classpath.
| Environment set to test.....
| Packaging Grails application.....
| Packaging Grails application.....
| Compiling 1 source files.
| Running 3 integration tests... 1 of 3
| Running 3 integration tests... 2 of 3
| Failure: testHandleLoginInvalidUser(sample.UserControllerTests)
| java.lang.NullPointerException: Cannot invoke method getBean() on null object
at grails.test.mixin.web.ControllerUnitTestMixin$_mockController_closure3.doCall(ControllerUnitTestMixin.groovy:304)
at grails.test.mixin.web.ControllerUnitTestMixin.mockController(ControllerUnitTestMixin.groovy:311)
| Failure: testHandleLoginInvalidUser(sample.UserControllerTests)
| java.lang.NullPointerException: Cannot invoke method delete() on null object
at sample.UserControllerTests.tearDown(UserControllerTests.groovy:25)
| Running 3 integration tests... 3 of 3
| Failure: testLogout(sample.UserControllerTests)
| java.lang.NullPointerException: Cannot invoke method getBean() on null object
at grails.test.mixin.web.ControllerUnitTestMixin$_mockController_closure3.doCall(ControllerUnitTestMixin.groovy:304)
at grails.test.mixin.web.ControllerUnitTestMixin.mockController(ControllerUnitTestMixin.groovy:311)
| Failure: testLogout(sample.UserControllerTests)
| java.lang.NullPointerException: Cannot invoke method delete() on null object
at sample.UserControllerTests.tearDown(UserControllerTests.groovy:25)
| Failure: sample.UserControllerTests
| java.lang.NullPointerException: Cannot invoke method isActive() on null object
at grails.test.mixin.support.GrailsUnitTestMixin.shutdownApplicationContext(GrailsUnitTestMixin.groovy:234)

|完成 3 个集成测试,5 个在 980ms 内失败

最佳答案

您在集成测试中使用单元测试注释 - 这会导致很多问题。通常,在进行集成测试时,如果您想使用 JUnit3 样式的测试,或者不使用任何东西并使用 JUnit4 注释,或者使用 Spock 并扩展 IntegrationSpec,则扩展 GroovyTestCase。

至于 NPE,无论您使用的是适当的单元测试还是集成测试,您都需要自己管理 Controller 的依赖项,因为您使用 new 显式创建它并且不作为访问它预接线的 Spring bean。但是集成测试确实支持依赖注入(inject),因此只需在 Controller 中为您需要的任何内容添加测试字段,并且在 setUp 或您可以在 Controller 中设置这些 bean 的单独方法中,例如

class UserControllerTests extends GroovyTestCase {

def grailsApplication
def someSpringBean
def someOtherSpringBean
private UserController uc = new UserController()

protected void setUp() {
super.setUp()
user = new User(userName: "User1", firstName: "User1FN", lastName: "User1LN").save()

//Set up UserController
uc.applicationContext = grailsApplication.mainContext
uc.someSpringBean = someSpringBean
uc.someOtherSpringBean = someOtherSpringBean
}
...

另请注意,您不需要在 tearDown() 中清理数据 - 集成测试在测试方法结束时回滚的事务中运行。

关于Grails 集成测试 - getBean() 的空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20622001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com