Grails单元测试异常java.lang.Exception:没有找到匹配Grails测试目标模式过滤器的测试



我刚刚开始学习Grails测试,并尝试编写我的第一个Grails测试。为此,我创建了一个新的grails项目,并创建了一个名为com.rahulserver.SomeController的控制器:

package com.rahulserver
class SomeController {
    def index() { }
    def someAction(){
    }
}

当我创建这个控制器时,grails在test/unit文件夹下自动创建了一个com.rahulserver. somecontrolerspec。下面是我的somecontrolerspec .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:115Labsgrailsunittestdemotargettest-reports
Error |
Forked Grails VM exited with error
Process finished with exit code 1

那它为什么会失败呢?

编辑

使用grails 2.4.3

默认情况下使用Spock定义单元测试:

void testSomeAction() {
    assert 1==1
}

应该写成:

void "Test some action"() {
    expect:
      1==1
}

见http://spockframework.github.io/spock/docs/1.0/index.html

最新更新