grails - 如果从Eclipse(STS)运行,则测试成功,但如果从带有ClassNotFoundException的grails运行,则测试将失败

标签 grails sts-springsourcetoolsuite grails-2.3

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

在grails下我的测试错误(即什至不运行)与ClassNotFoundException

在 eclipse 中,它们运行成功。

(从grails FWIW运行时,运行应用程序仍然可以正常工作。)

测试已经在两种环境中进行了几天。
然后,我删除了一些我不需要的文件(域, Controller 及其上的单元测试)
现在我有一个问题。

版本:

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

从eclipse / STS内部看,一切仍然很好
select project
run as > junit test
runs 24 tests in 3 test classes
all succeed

grails 2.3.6

但是从终端的内部测试不再运行。
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,它仍然建议我添加--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的清理和全部清理。然后,test-app重新编译所有内容,但没有任何区别-eclipse仍然可以成功运行测试,并且在没有运行任何测试的情况下仍然产生错误;我按哪个顺序运行都没关系。

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

我该怎么办?

最佳答案

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

由于stacktrace仅显示了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。

关于grails - 如果从Eclipse(STS)运行,则测试成功,但如果从带有ClassNotFoundException的grails运行,则测试将失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22071696/

相关文章:

java - 在 Tomcat 上远程编辑 SpringSource 应用程序

excel - Grails 2.3.7 中损坏的导出插件

grails - 调用 Grails 2 r :require module from a taglib

maven - Grails Maven安装在Windows 7上不起作用

grails - 根据某些条件取消/阻止域对象的保存?

sql-server - 找不到适用于jdbc:sqlserver的驱动程序

grails - 如何在Grails中注册自定义json解串器/解析器?

java - Groovy 项目不会在 GGTS 中构建

grails - def函数/Grails中的方法

javascript - Spring Source Tool Suite(等于 Eclipse IDE)javascript "Open Declaration"不可能