我最近开始认真地使用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)
有没有人有任何想法?
答案 0 :(得分:0)
你不是@Mock
你的EndUser课程所以我不确定你是如何让它在第一次测试中工作的。
我会在您的课程中添加注释,看看是否可以修复它:
@TestFor(OrderController)
@Mock([EndUser])
class OrderControllerIntegrationTests{
答案 1 :(得分:0)
@TestFor仅用于单元测试。
您可以通过执行以下操作注入控制器
class OrderControllerIntegrationTests extends GroovyTestCase {
OrderController orderController
EndUser user
@Before
void initialize() {
user = new EndUser(username: UUID.randomUUID().toString(), password: "secret");
user.save()
}
}