I'm getting different test results when run from eclipse (STS) and from grails.

Under grails my tests error (ie dont even run) with a ClassNotFoundException

In eclipse they run successfully.

(And run-app still works fine when run from grails FWIW.)

Tests had been working in both environments for several days. Then I deleted a number of files I didn't need (domains. controllers and the unit tests on them) Now I have a problem.

Versions:

ubuntu 10.04
eclipse
eclipse / SpringToolSuite 3.4.0
 - groovy compiler: groovy 2.07
 - grails location: /home/nick/grails-2.3.6
 - JDK compliance level 1.6
 - JAVA_HOME=/usr/lib/jvm/java-6-openjdk
grails 2.3.6
 - GRAILS_HOME=/home/nick/grails-2.3.6
 - JAVA_HOME=/usr/lib/jvm/java-6-openjdk

eclipse / SpringToolSuite 3.4.0

Everything is still fine and dandy from inside eclipse / STS

select project
run as > junit test
runs 24 tests in 3 test classes
all succeed

grails 2.3.6

But from inside grails in a terminal the tests no longer run.

grails> test-app
| Compiling 2 source files.
| Error Fatal error running tests: Could not load class in test type 'unit' (Use --stacktrace to see the full trace)
| Compiling 2 source files..
| Tests FAILED  - view reports in /home/nick/grails-2.3.6-workspace/imca2/target/test-reports

Browsing the test-reports shows: No tests executed.

Adding --stacktrace makes no difference, no stacktrace is provided and it still advises me to add --stacktrace.

grails test-app --stacktrace | tee /tmp/out

gives

| Loading Grails 2.3.6
| Configuring classpath
| Configuring classpath.
| Environment set to test
| Environment set to test.
| Environment set to test..
| Environment set to test...
| Environment set to test....
| Environment set to test.....
| Running without daemon...
| Compiling 1 source files
| Compiling 1 source files.
| Error Fatal error running tests: Could not load class in test type 'unit' (NOTE: Stack trace has been filtered. Use --verbose to see entire trace.)
java.lang.RuntimeException: Could not load class in test type 'unit'
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1254)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1254)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1254)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1254)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1254)
Caused by: java.lang.ClassNotFoundException: com.ubergen.AdvocacyStaffSpec
    ... 5 more
| Error Fatal error running tests: Could not load class in test type 'unit'
| Compiling 1 source files..
| Tests FAILED  - view reports in /home/nick/grails-2.3.6-workspace/imca2/target/test-reports
| Error Error running forked test-app: Could not load class in test type 'unit' (NOTE: Stack trace has been filtered. Use --verbose to see entire trace.)
java.lang.RuntimeException: Could not load class in test type 'unit'
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1254)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1254)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1254)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1254)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1254)
Caused by: java.lang.ClassNotFoundException: com.ubergen.AdvocacyStaffSpec
    ... 5 more
| Error Error running forked test-app: Could not load class in test type 'unit'
| Error Forked Grails VM exited with error

I've tried grails clean and clean-all first. test-app then recompiles everything, but it makes no difference - eclipse still runs the tests successfully and grails still errors without running any of them; and it doesn't matter which order I run them in.

Nor does it make any difference which order I run tests in.

What should I do?

有帮助吗?

解决方案

In case it helps anyone, I'm posting my own answer after tracking problem down.

As the stacktrace showed only one of the spock tests. Grails could run the others successfully.

On closer inspection the failing test turned out to have a mistyped class name. I hadn't looked closely enough as its name is only a convention, or so I thought.

So, assuming there is a class CorrectName that needs testing but the test is mistyped as WrongNameSpec instead of CorrectNameSpec...

Here is the mistyped test code in CorrectName.groovy:

[snip]
@TestFor(CorrectName)
class WrongNameSpec extends Specification {
[snip]

This runs in eclipse/STS and tests the CorrectName class successfully.

In grails it fails with a stacktrace that says:

| Error Fatal error running tests: Could not load class in test type 'unit'
Caused by: java.lang.ClassNotFoundException: com.ubergen.CorrectNameSpec

Grails and eclipse have different target areas to put the compiled code in. Here the class files are:

./target-eclipse/classes/com/ubergen/WrongNameSpec.class
./target/test-classes/unit/com/ubergen/WrongNameSpec.class

As the stack trace said, there is no CorrectNameSpec class.

This is the corrected code and runs ok in both:

[snip]
@TestFor(CorrectName)
class CorrectName extends Specification {
[snip]

Grails is doing something like building a list of tests from the @TestFor but assuming the test classname will follow the expected convention.

Summary:

In eclipse/sts the name of the unit test is a convention, its the @TestFor that matters. In grails the name of the unit test is not just a convention, you must name it as expected, because grails will assume there is a class file that matches that name and you will get a ClassNotFoundException.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top