Вопрос

I have a fairly trivial Grails unit test:

@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()
    }
}

When I try to launch it with 'Run' from my IntelliJ Idea 13 EAP I get:

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)

The line in question in GrailsUnitTestMixin is:

     static void shutdownApplicationContext() {
         if (applicationContext.isActive()) { <-- this one

When I launch it with grails test-app unit: or grails test-app :spock I get:

| Tests PASSED - view reports in C:\projects\MyProject\target\test-reports

However - the reports there actually show No tests executed., despite the fact that the test is in /test/unit/mypackage/ where it was automatically created by Grails. If I add a print statement into the test it also doesn't get executed.

What am I doing wrong?


Edit - Burt Beckwith suggested my unit test is wrong. He's right, but that doesn't actually fully solve the problem I'm having :/

    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()
    }

gets

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)

Whether due to this or otherwise, I'm getting different behavior as well from command line 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)

Edit #2 - commenting out webdriver in build config / restarting grails / clean-all helped with last issue, but test-app :spock still shows as "No tests executed.' in the HTML report.

Это было полезно?

Решение

This is a Spock test, and test methods have to have one or more labelled blocks to be considered test methods. Rename the class to end in "Tests" and extend GroovyTestCase (or a subclass of it) to use JUnit 3, or extend nothing to use JUnit 4. Or convert the test to Spock format:

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

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

Другие советы

I hit this error when I used @TestFor on an IntegrationSpec. Instead, use:

class MyControllerSpec extends IntegrationSpec {
    @Autowired
    MyController controller

    void "my controller does something"() {
        ...
    }
}

Make sure you are using Specification in the /unit test path and IntegrationSpec in the /integration path.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top