0

我最近开始认真地使用 Grails 2.0,但我遇到了一个对我来说没有多大意义的障碍。我已经编写了一个集成测试,它对控制器使用 @TestFor 注释,但是我从中得到了非常奇怪的错误。以下是测试,精简到重复此问题的最小情况:

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

@TestFor(OrderController)
class OrderControllerIntegrationTests {

    public EndUser user

    @Before public void initialize() {
        user = new EndUser(username: UUID.randomUUID().toString(), password: "secret");
        user.save()
    }

    @Test public void get() {
        assert true
    }

    @Test public void create() {
        assert true
    }

}

总之,我得到的输出是我的测试设置中的 save() 方法不再存在于此类中的第二个测试。这似乎……很奇怪。

以下是 grails test-app exampleapp.OrderControllerIntegrationTests 的完整输出:

Testsuite: exampleapp.OrderControllerIntegrationTests
Tests run: 2, Failures: 0, Errors: 1, Time elapsed: 0.769 sec
------------- Standard Output ---------------
--Output from get--
2012-05-15 22:35:03,491 [main] DEBUG transaction.JDBCTransaction  - begin

2012-05-15 22:35:03,493 [main] DEBUG transaction.JDBCTransaction  - current autocommit status: true

2012-05-15 22:35:03,493 [main] DEBUG transaction.JDBCTransaction  - disabling autocommit

2012-05-15 22:35:03,689 [main] DEBUG hibernate.SQL  - insert into widget_user (id, version, password, username, class) values (null, ?, ?, ?, 'exampleapp.EndUser')

2012-05-15 22:35:03,970 [main] DEBUG transaction.JDBCTransaction  - rollback

2012-05-15 22:35:03,970 [main] DEBUG transaction.JDBCTransaction  - re-enabling autocommit

2012-05-15 22:35:03,970 [main] DEBUG transaction.JDBCTransaction  - rolled back JDBC Connection

--Output from create--
2012-05-15 22:35:03,979 [main] DEBUG transaction.JDBCTransaction  - begin

2012-05-15 22:35:03,979 [main] DEBUG transaction.JDBCTransaction  - current autocommit status: true

2012-05-15 22:35:03,979 [main] DEBUG transaction.JDBCTransaction  - disabling autocommit

2012-05-15 22:35:04,207 [main] DEBUG transaction.JDBCTransaction  - rollback

2012-05-15 22:35:04,207 [main] DEBUG transaction.JDBCTransaction  - re-enabling autocommit

2012-05-15 22:35:04,207 [main] DEBUG transaction.JDBCTransaction  - rolled back JDBC Connection

------------- ---------------- ---------------
------------- Standard Error -----------------
--Output from get--
--Output from create--
------------- ---------------- ---------------

Testcase: get took 0.51 sec
Testcase: create took 0.251 sec
    Caused an ERROR
No signature of method: exampleapp.EndUser.save() is applicable for argument types: () values: []
Possible solutions: save(), save(), save(boolean), save(java.util.Map), save(boolean), save(java.util.Map)
groovy.lang.MissingMethodException: No signature of method: exampleapp.EndUser.save() is applicable for argument types: () values: []
Possible solutions: save(), save(), save(boolean), save(java.util.Map), save(boolean), save(java.util.Map)
    at exampleapp.OrderControllerIntegrationTests.initialize(OrderControllerIntegrationTests.groovy:17)

有没有人有任何想法?

4

2 回答 2

0

@TestFor 仅用于单元测试。

您可以通过执行以下操作注入控制器

class OrderControllerIntegrationTests extends GroovyTestCase {
    OrderController orderController
    EndUser user

    @Before
    void initialize() {
        user = new EndUser(username: UUID.randomUUID().toString(), password: "secret");
        user.save()
    }
}
于 2014-03-26T15:13:30.283 回答
0

您没有使用@MockEndUser 类,所以我不确定您是如何让它在第一次测试中工作的。

我会将注释添加到您的课程中,看看是否可以解决它:

@TestFor(OrderController)
@Mock([EndUser])
class OrderControllerIntegrationTests{
于 2012-05-16T15:52:33.827 回答