开发者

JUnit testing groovy scripts results in "unable to resolve class" from the JUnit test file which attempts to test another groovy script

I am running groovy by embedding it in a Java application via the following code:

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);
  }

Now I have another groovy script with the code below (which i execute on a button click). Again the code is simplified but it works correctly and the started script can see and use the file I am trying to JUnit test without issues:

GroovyScriptEngine gse = new GroovyScriptEngine(roots);
gse.run(relativeScriptName, binding);

The script I am executing has the following code:

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())

Now what I am noticing is that even the above script (AllTests) can import and use the file I want to JUnit test, but the the JUnit test itself fails with an "unable to resolve class" exception when the TestRunner above runs the JUnit test. And it fails when i just import the class. The JUnit test i'm running is summarized below:

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"
   }

}

Note that the JUnit test passes and runs correctly if i don't import the class i want to te开发者_如何学JAVAst. But the JUnit test isn't much use to me unless I can use it to test my other groovy classes and scripts.

Any thoughts?


Ok I figured out the issue. For some reason I need to compile all the groovy scripts that my tests will use even though I don't have to compile any of them if I just call them using groovy (ie. standard groovy usage). I hope this saves other people some time in debugging. To compile a file just use

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

in my code above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜