我刚刚开始学习Grails测试,我尝试编写我的第一个grails测试。为此,我创建了一个新的grails项目并创建了一个名为com.rahulserver.SomeController的控制器:
package com.rahulserver
class SomeController {
def index() { }
def someAction(){
}
}
当我创建这个控制器时,grails会自动在test / unit文件夹下创建一个com.rahulserver.SomeControllerSpec。 这是我的SomeControllerSpec.groovy:
package com.rahulserver
import grails.test.mixin.TestFor
import spock.lang.Specification
/**
* See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
*/
@TestFor(SomeController)
class SomeControllerSpec extends Specification {
def setup() {
}
def cleanup() {
}
void testSomeAction() {
assert 1==1
}
}
当我右键单击此类并运行此测试时,我得到以下信息:
Testing started at 5:21 PM ...
|Loading Grails 2.4.3
|Configuring classpath
.
|Environment set to test
....................................
|Running without daemon...
..........................................
|Compiling 1 source files
.
|Running 1 unit test...|Running 1 unit test... 1 of 1
--Output from initializationError--
Failure: |
initializationError(org.junit.runner.manipulation.Filter)
|
java.lang.Exception: No tests found matching grails test target pattern filter from org.junit.runner.Request$1@1f0f9da5
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:35)
at org.junit.runner.JUnitCore.run(JUnitCore.java:138)
No tests found matching grails test target pattern filter from org.junit.runner.Request$1@1f0f9da5
java.lang.Exception: No tests found matching grails test target pattern filter from org.junit.runner.Request$1@1f0f9da5
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:35)
at org.junit.runner.JUnitCore.run(JUnitCore.java:138)
|Completed 1 unit test, 1 failed in 0m 0s
.Tests FAILED
|
- view reports in D:\115Labs\grailsunittestdemo\target\test-reports
Error |
Forked Grails VM exited with error
Process finished with exit code 1
那为什么会失败呢?
编辑
我正在使用grails 2.4.3
答案 0 :(得分:16)
单位测试默认使用Spock定义:
void testSomeAction() {
assert 1==1
}
应写成:
void "Test some action"() {
expect:
1==1
}
请参阅http://spockframework.github.io/spock/docs/1.0/index.html