项目场景:
Jmeter接口自动化测试,因登录、设置密码等相关接口需要进行签名、请求数据需要利用算法进行加密处理,因此需要将相关实现封装成jar包,通过BeanShell调用jar包实现相关处理。
问题描述
java代码使用的是maven项目,因代码中用到了许多第三方库,在进行打包时需要将第三方包一起打包进jar包,对于java小白的我来说只能是在网上搜索一些pom.xml中需要的build相关配置,打包完成后通过BeanShell进行调用,报错如下:
2024-01-24 15:38:03,213 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import com.xxx.common.Login; Map headers = new HashMap(); headers.put("xxx . . . '' : Typed variable declaration : Method Invocation Login.getVerifyCode
2024-01-24 15:38:03,213 WARN o.a.j.p.j.s.BeanShellSampler: Exception executing script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import com.xxx.common.Login; Map headers = new HashMap(); headers.put("xxx . . . '' : Typed variable declaration : Method Invocation Login.getVerifyCode
BeanShell中代码实现
import com.xxx.common.Login;
Map headers = new HashMap();
headers.put("xxx","xxx");
String phone = "13271081234";
String url= "xxxxx/login/verify-code";
String code = Login.getVerifyCode(url,headers,phone);
log.info(code);
pom.xml中build相关配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<!--wy -->
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<!--wy -->
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
原因分析:
暂未找到具体原因,如果哪位朋友看见分享,可以留言告诉我问题原因,不胜感谢。
解决方案:
换了一种实现方式,不使用Maven,直接创建Java项目,Build system选择IntelliJ,将使用到的第三方库直接放在项目中自己创建的lib目录下,将该目录设置为Project Library,进行打包,然后在BeanShell中引用该jar,可以成功调用jar包中的接口。