我被通过下面的代码在Java应用程序中嵌入它运行常规JUnit测试文件测试Groovy脚本结果“无法解析类”:的JUnit从中试图测试另一个Groovy脚本
GroovyScriptEngine gse = null;
try {
//note that roots is properly defined above i just didn't include it in this example so that it
//remains concise
gse = new GroovyScriptEngine(roots);
} catch (IOException ex) {
Logger.getLogger(GroovyWrapper.class.getName()).log(Level.SEVERE, null, ex);
}
现在我有另一个groovy脚本与下面的代码(我点击按钮执行)。同样的代码是简单,但它正常工作和启动脚本可以看到和使用的文件我想JUnit测试没有问题:
GroovyScriptEngine gse = new GroovyScriptEngine(roots);
gse.run(relativeScriptName, binding);
我执行该脚本有以下代码:
class AllTests extends TestSuite {
static TestSuite suite() {
TestSuite suite = new TestSuite();
GroovyTestSuite gsuite = new GroovyTestSuite();
suite.addTestSuite(gsuite.compile("LibraryTest.groovy"));
return suite;
}
}
TestRunner.run(AllTests.suite())
现在我注意到,即使上面的脚本(AllTests)也可以导入并使用我想要JUnit测试的文件,但是当上面的TestRunner运行时,JUnit测试本身会失败,出现“无法解析类”异常JUnit测试。当我刚刚导入课程时,它会失败。 JUnit测试我正在总结如下:
package lib
import groovy.util.GroovyTestCase
import lib.Dictionary;
class LibraryTest extends GroovyTestCase {
public void testSomething() {
assert 1 == 1
assert 2 + 2 == 4 : "We're in trouble, arithmetic is broken"
}
}
注意,JUnit测试通过,运行正常,如果我不导入我想测试的类。但是JUnit测试对我来说没有什么用处,除非我可以用它来测试我的其他常规类和脚本。
有什么想法?