Jmeter——引入第三方jar包

前言

网上已经有写关于jmeter引入第三方jar包的文章,本文主要是记录遇到报错进行排查并解决的文章。

编写java类,并实现两个方法。一个方法需要用到第三方jar包,在这用maven作引用。

maven编译打包

jmeter引入jar包

尝试在Bean Shell预处理程序中使用我们得java类

处理jmeter Bean Shell异常:(Error invoking bsh method: eval Sourced file: inline evaluation of)

(1)我们尝试调用TestCase类中的show()方法看看

(2)通过上一步,发现是可以调用到方法的,那么我们去代码看看是因为什么问题。

(3)通过分析,可以想到是因为引用了第三方Jar包,maven编译时并没有将依赖的jar包引入,导致jmeter找不到对应的jar包。那么我们可以通过maven编译将依赖打包进去尝试一下。

    <build>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>2.15.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</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>2.3</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <!--这里写上TestCase的类路径,如果写入无效,在TestCase类中增加一个main方法 -->
                                    <mainClass>com.develop.TestCase</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <defaultGoal>compile</defaultGoal>
    </build>

(4)再次在Bean shell使用show1()方法,并输出看看结果。

原文地址:https://www.cnblogs.com/se7enjean/p/15157644.html