0

从 eclipse (STS) 和 grails 运行时,我得到不同的测试结果。

在 grails 下,我的测试错误(即甚至不运行)带有 ClassNotFoundException

在eclipse中他们成功运行。

(从 grails FWIW 运行时,run-app 仍然可以正常工作。)

测试已经在这两种环境中进行了几天。然后我删除了一些我不需要的文件(域。控制器和它们的单元测试)现在我有一个问题。

版本:

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

日食/SpringToolSuite 3.4.0

从 Eclipse / STS 内部看,一切都很好

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

圣杯 2.3.6

但是从终端的 grails 内部,测试不再运行。

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

浏览测试报告显示:未执行任何测试。

添加--stacktrace 没有区别,没有提供堆栈跟踪,它仍然建议我添加--stacktrace。

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

| 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

我首先尝试过 grails clean and clean-all。然后 test-app 重新编译所有内容,但这没有什么区别 - eclipse 仍然成功运行测试,grails 仍然错误而不运行任何测试;我运行它们的顺序并不重要。

我运行测试的顺序也没有任何区别。

我应该怎么办?

4

1 回答 1

1

如果它对任何人有帮助,我会在跟踪问题后发布我自己的答案。

由于堆栈跟踪仅显示了一项 spock 测试。Grails 可以成功运行其他系统。

经过仔细检查,失败的测试结果是一个错误的类名。我没有仔细看,因为它的名字只是一个约定,或者我是这么认为的。

因此,假设有一个类 CorrectName 需要测试,但测试被错误输入为 WrongNameSpec 而不是 CorrectNameSpec ...

这是 CorrectName.groovy 中输入错误的测试代码:

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

这在 eclipse/STS 中运行并成功测试了 CorrectName 类。

在 grails 中,它失败并显示堆栈跟踪:

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

Grails 和 eclipse 有不同的目标区域来放置编译后的代码。这里的类文件是:

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

正如堆栈跟踪所说,没有 CorrectNameSpec 类。

这是更正后的代码,并且在两者中都可以正常运行:

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

Grails 正在做一些事情,比如从 @TestFor 构建一个测试列表,但假设测试类名将遵循预期的约定。

概括:

在 eclipse/sts 中,单元测试的名称是一个约定,它的 @TestFor 很重要。在 grails 中,单元测试的名称不仅仅是一个约定,您必须按预期命名它,因为 grails 将假定有一个与该名称匹配的类文件,您将收到 ClassNotFoundException。

于 2014-03-01T10:24:15.240 回答