为什么我的单元测试不起作用?

Joh*_*n M 5 grails unit-testing spock

我有一个相当琐碎的Grails单元测试:

@TestFor(DateTimeTagLib)
class DateTimeTagLibSpec extends Specification {
    def setup() {
    }

    def cleanup() {
    }

    void "showTime"() {
        assertEquals "14:26", new DateTimeTagLib().showTime(value: DateTime.parse("2013-01-01 14:26:00")).toString()
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用IntelliJ Idea 13 EAP中的'Run'启动它时,我得到:

java.lang.NullPointerException
at grails.test.mixin.support.GrailsUnitTestMixin.shutdownApplicationContext(GrailsUnitTestMixin.groovy:266)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.runtime.extension.builtin.JUnitFixtureMethodsExtension$FixtureType$FixtureMethodInterceptor.intercept(JUnitFixtureMethodsExtension.java:145)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Run Code Online (Sandbox Code Playgroud)

GrailsUnitTestMixin中的相关行是:

     static void shutdownApplicationContext() {
         if (applicationContext.isActive()) { <-- this one
Run Code Online (Sandbox Code Playgroud)

当我启动它grails test-app unit:grails test-app :spock我得到:

| Tests PASSED - view reports in C:\projects\MyProject\target\test-reports
Run Code Online (Sandbox Code Playgroud)

然而 - 实际上的报告显示No tests executed.,尽管测试是在/test/unit/mypackage/Grails自动创建的地方.如果我在测试中添加一个print语句,它也不会被执行.

我究竟做错了什么?


编辑 - Burt Beckwith建议我的单元测试是错误的.他是对的,但这实际上并没有完全解决我遇到的问题:/

    void "showTime"() {
        given:
            print 'executing'
            def taglib = new DateTimeTagLib()

        expect:
            "14:26" == taglib.showTime(value: DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("2013-01-01 14:26:00")).toString()
    }
Run Code Online (Sandbox Code Playgroud)

得到

executing
java.lang.NullPointerException
at grails.test.mixin.support.GrailsUnitTestMixin.shutdownApplicationContext(GrailsUnitTestMixin.groovy:266)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.runtime.extension.builtin.JUnitFixtureMethodsExtension$FixtureType$FixtureMethodInterceptor.intercept(JUnitFixtureMethodsExtension.java:145)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Run Code Online (Sandbox Code Playgroud)

无论是由于这个原因还是其他原因,我在命令行中也会遇到不同的行为test-app :spock.

grails> test-app :spock
| Error An error occurred installing the plugin [webdriver-0.4.2]: Unable to delete file C:\projects\myproject\target\work\plugins\webdriver-0.4.2\lib\webdriver-ast-transforms.jar
| Error Error running script test-app :spock: org.codehaus.groovy.grails.cli.ScriptExitException (Use --stacktrace to see the full trace)
Run Code Online (Sandbox Code Playgroud)

编辑#2 - 在构建配置中注释掉webdriver /重启grails/clean-allhelp with last issue,但test-app :spock仍然显示为"没有执行测试." 在HTML报告中.

Bur*_*ith 5

这是一种 Spock 测试,测试方法必须有一个或多个标记的块才能被视为测试方法。将类重命名为以“Tests”结尾并扩展 GroovyTestCase(或其子类)以使用 JUnit 3,或不扩展任何内容以使用 JUnit 4。或将测试转换为 Spock 格式:

void "showTime"() {
   given:
      def taglib = new DateTimeTagLib()

   expect:
      "14:26" == taglib.showTime(value: DateTime.parse("2013-01-01 14:26:00")).toString()
}
Run Code Online (Sandbox Code Playgroud)