我花了很多时间寻找答案。我找到了类似解决方案here的东西,但这是错误的。
情况:
我有2个groovy脚本和Java应用程序。
Another.groovy(自动测试/源)
class Another
{
protected String name="";
public Another() {}
public main(String[] args) {}
public boolean getResult() {return true;}
public String getName() {return name;}
public void setName(String value) {name=value;}
}
test.groovy(自动测试/案例)
evaluate(new File("autotest/sources/Another.groovy"))
import support.tool.AutotestResult;
public class Another2 extends Another
{
public Another2()
{
this.setName(this.name+"N");
}
public AutotestResult run()
{
return new AutotestResult(this.name+"123",this.getResult(),null,null)
}
}
Another2 a = new Another2()
a.run()
名为“ test.groovy”的Java类
String[] paths = {"autotest\\cases\\test.groovy"};
GroovyScriptEngine gse = new GroovyScriptEngine(paths);
Binding binding = new Binding();
binding.setVariable("args",null);
System.out.println(((AutotestResult)gse.run("test.groovy", binding)).toJSON());
如果Another.groovy和test.groovy位于同一文件夹中,则可以完美地工作。但是,如果Another.groovy在另一个文件夹中,则它将不起作用。 Java返回错误:
Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
file:/.../autotest/cases/test.groovy: 6: unable to resolve class Another
@ line 6, column 1.
public class Another2 extends Another
^
所以我有问题:
PS。对不起,英语不好。
答案 0 :(得分:0)
导入问题已解决
解决方案:
Another.groovy(自动测试/来源)
package sources
public class Another
{
protected String name="AnotherNama";
public Another() {}
public main(String[] args) {}
public boolean getResult() {return true;}
public String getName() {return name;}
public void setName(String value) {name=value;}
}
test.groovy(自动测试/案例)
package cases
import support.tool.AutotestResult;
import sources.Another
public class Another2 extends Another
{
public Another2()
{
this.setName(this.name+"N");
}
public AutotestResult run()
{
return new AutotestResult(this.name+"123",this.getResult(),null,null)
}
}
Another2 a = new Another2()
a.run()
Java代码:
CompilerConfiguration config=new CompilerConfiguration();
config.setClasspath("autotest");
config.addCompilationCustomizers(new ImportCustomizer());
GroovyShell shell=new GroovyShell(config);
Binding binding = new Binding();
binding.setVariable("args",null);
System.out.println(((AutotestResult)shell.run(new File("autotest/cases/test.groovy"),new ArrayList())).toJSON());