【问题标题】:JUnit testing groovy scripts results in "unable to resolve class" from the JUnit test file which attempts to test another groovy scriptJUnit 测试 groovy 脚本导致 JUnit 测试文件中的“无法解析类”,该文件试图测试另一个 groovy 脚本
【发布时间】:2011-09-11 22:54:27
【问题描述】:

我通过以下代码将 groovy 嵌入到 Java 应用程序中来运行它:

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 测试对我来说用处不大,除非我可以用它来测试我的其他 groovy 类和脚本。

有什么想法吗?

【问题讨论】:

    标签: java testing groovy


    【解决方案1】:

    好的,我发现了这个问题。出于某种原因,我需要编译我的测试将使用的所有 groovy 脚本,即使如果我只是使用 groovy 调用它们(即标准 groovy 用法),我也不必编译它们中的任何一个。我希望这可以节省其他人调试的时间。要编译一个文件,只需使用

    gsuite.compile("<file name>");
    

    在我上面的代码中。

    【讨论】:

      猜你喜欢
      • 2011-04-17
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多