简体   繁体   English

在 SoapUI Groovy 中调用 Java 类

[英]Call Java class in SoapUI Groovy

I am trying to call java class from Groovy script in SoapUI我正在尝试从 SoapUI 中的 Groovy 脚本调用 java 类

My Java Class我的 Java 类

public class LogTest {
    public void sayHello (){
        System.out.println("Hello");
    }   
}

Groovy script Groovy 脚本

import LogTest;

new LogTest().sayHello()

I places the LogTest.class in ...SmartBear\SoapUI-4.6.1\bin\ext path and restart SoapUI我将 LogTest.class 放在...SmartBear\SoapUI-4.6.1\bin\ext路径中并重新启动 SoapUI

But still I am getting following error in SoapUI但我仍然在 SoapUI 中遇到以下错误

:ERROR:org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script5.groovy: 1: unable to resolve class LogTest
 @ line 1, column 1.
   import LogTest;
   ^
org.codehaus.groovy.syntax.SyntaxException: unable to resolve class LogTest
 @ line 1, column 1.
    at org.codehaus.groovy.ast.ClassCodeVisitorSupport.addError(ClassCodeVisitorSupport.java:148)
    at org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:1240)
    at org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:148)
    at org.codehaus.groovy.control.CompilationUnit$8.call(CompilationUnit.java:601)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:839)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:544)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:493)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:306)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:287)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:731)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:743)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:770)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:761)
    at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.compile(SoapUIGroovyScriptEngine.java:148)
    at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:93)
    at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:149)
    at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.runTestStep(AbstractTestCaseRunner.java:239)
    at com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner.runCurrentTestStep(WsdlTestCaseRunner.java:48)
    at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.internalRun(AbstractTestCaseRunner.java:148)
    at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.internalRun(AbstractTestCaseRunner.java:43)
    at com.eviware.soapui.impl.wsdl.support.AbstractTestRunner.run(AbstractTestRunner.java:135)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

What could be the possible root cause可能的根本原因是什么

Cheers干杯

  1. Build a jar file containing your code (LogTest)构建一个包含您的代码的 jar 文件 (LogTest)

  2. Put that jar file in the /bin/ext directory.将该 jar 文件放在 /bin/ext 目录中。

This will allow you to call your code from a Groovy script.这将允许您从 Groovy 脚本调用您的代码。

Were you able to resolve this issue?你能解决这个问题吗? If not I ran into the same scenario, and here is how I fixed it.如果不是,我遇到了同样的情况,这就是我修复它的方法。

I had a jar file (HelloWorldTest.jar) with the following information:我有一个包含以下信息的 jar 文件(HelloWorldTest.jar):

package helloworldtest;

public class HWT {

    public static String Testing() {
        return "Hello World!";
    }

}

Notice that my file name and package name are different.请注意,我的文件名和包名不同。 Make sure when importing into Groovy you use the package name.确保在导入 Groovy 时使用包名。

I used:我用了:

import helloworldtest.HWT;

log.info HWT.Testing();

Result would be:结果将是:

Thu Mar 06 16:26:01 PST 2014:INFO:Hello World!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM