【问题标题】:JMeter fails with ClassNotFoundException when run in Maven在 Maven 中运行时,JMeter 失败并出现 ClassNotFoundException
【发布时间】:2015-01-07 16:46:23
【问题描述】:

我创建了一个 JMeter 测试文件,它运行我用 java 编写的一些测试

当我在 JMeter 中运行文件时,它们运行良好并且测试通过。现在我已经将 maven 配置为运行 JMX 文件,并且当我运行验证目标时,它会执行 JMX 文件。但是现在它失败并显示以下错误消息:

JMeter.jmx.log: "jmeter.protocol.java.sampler.JUnitSampler: ClassNotFoundException:: com.mynamespace.tests.product.ProductTest"

Jmeter.jtl:“无法创建 com.mynamespace.tests.product.ProductTest 的实例,原因可能是缺少空构造函数和一个字符串构造函数,或者无法实例化构造函数,请检查 jmeter 日志文件中的警告消息”

为了让 JMeter 知道该类,我构建了一个 jar 并将其复制到 Jmeter 的 lib 目录中。运行 jmeter maven 插件时我需要做类似的事情吗? - 在运行验证目标时,它应该能够看到项目构建时的类..

添加到 pom.xml:

            <plugin>
          <groupId>com.lazerycode.jmeter</groupId>
          <artifactId>jmeter-maven-plugin</artifactId>
          <version>1.9.1</version>
          <executions>
              <execution>
                  <id>jmeter-tests</id>
                  <phase>verify</phase>
                  <goals>
                      <goal>jmeter</goal>
                  </goals>
             </execution>
          </executions>
      </plugin>

Jmeter.jmx:

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.6" jmeter="2.11 r1554548">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
  <stringProp name="TestPlan.comments"></stringProp>
  <boolProp name="TestPlan.functional_mode">false</boolProp>
  <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
  <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
    <collectionProp name="Arguments.arguments"/>
  </elementProp>
  <stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
  <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
    <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
    <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
      <boolProp name="LoopController.continue_forever">false</boolProp>
      <stringProp name="LoopController.loops">1</stringProp>
    </elementProp>
    <stringProp name="ThreadGroup.num_threads">1</stringProp>
    <stringProp name="ThreadGroup.ramp_time">1</stringProp>
    <longProp name="ThreadGroup.start_time">1408690229000</longProp>
    <longProp name="ThreadGroup.end_time">1408690229000</longProp>
    <boolProp name="ThreadGroup.scheduler">false</boolProp>
    <stringProp name="ThreadGroup.duration"></stringProp>
    <stringProp name="ThreadGroup.delay"></stringProp>
  </ThreadGroup>
  <hashTree>
    <JUnitSampler guiclass="JUnitTestSamplerGui" testclass="JUnitSampler" testname="JUnit Request" enabled="true">
      <stringProp name="junitSampler.classname">com.mynamespace.tests.product.ProductTest</stringProp>
      <stringProp name="junitsampler.constructorstring"></stringProp>
      <stringProp name="junitsampler.method">saveProduct</stringProp>
      <stringProp name="junitsampler.pkg.filter"></stringProp>
      <stringProp name="junitsampler.success">Test successful</stringProp>
      <stringProp name="junitsampler.success.code">1000</stringProp>
      <stringProp name="junitsampler.failure">Test failed</stringProp>
      <stringProp name="junitsampler.failure.code">0001</stringProp>
      <stringProp name="junitsampler.error">An unexpected error occured</stringProp>
      <stringProp name="junitsampler.error.code">9999</stringProp>
      <stringProp name="junitsampler.exec.setup">false</stringProp>
      <stringProp name="junitsampler.append.error">true</stringProp>
      <stringProp name="junitsampler.append.exception">true</stringProp>
      <boolProp name="junitsampler.junit4">true</boolProp>
    </JUnitSampler>
    <hashTree/>
    <JUnitSampler guiclass="JUnitTestSamplerGui" testclass="JUnitSampler" testname="JUnit Request" enabled="true">
      <stringProp name="junitSampler.classname">com.mynamespace.tests.product.ProductTest</stringProp>
      <stringProp name="junitsampler.constructorstring"></stringProp>
      <stringProp name="junitsampler.method">getProduct</stringProp>
      <stringProp name="junitsampler.pkg.filter"></stringProp>
      <stringProp name="junitsampler.success">Test successful</stringProp>
      <stringProp name="junitsampler.success.code">1000</stringProp>
      <stringProp name="junitsampler.failure">Test failed</stringProp>
      <stringProp name="junitsampler.failure.code">0001</stringProp>
      <stringProp name="junitsampler.error">An unexpected error occured</stringProp>
      <stringProp name="junitsampler.error.code">9999</stringProp>
      <stringProp name="junitsampler.exec.setup">false</stringProp>
      <stringProp name="junitsampler.append.error">true</stringProp>
      <stringProp name="junitsampler.append.exception">true</stringProp>
      <boolProp name="junitsampler.junit4">true</boolProp>
    </JUnitSampler>
    <hashTree/>
  </hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>

【问题讨论】:

    标签: java maven classnotfoundexception jmeter-maven-plugin


    【解决方案1】:

    您需要修改 pom.xml 文件以将您的 jar 工件作为依赖项包含在内。比如:

      <plugin>
          <groupId>com.lazerycode.jmeter</groupId>
          <artifactId>jmeter-maven-plugin</artifactId>
          <version>1.10.0</version>
          <executions>
              <execution>
                  <phase>verify</phase>
                  <id>jmeter-tests</id>
    
                  <goals>
                      <goal>jmeter</goal>
                  </goals>
              </execution>
          </executions>
          <dependencies>
              <dependency>
                  <groupId>com.mynamespace.tests.product</groupId>
                  <artifactId>com.mynamespace.tests.product.ProductTest</artifactId>
                  <version>1.0-SNAPSHOT</version>
              </dependency>
          </dependencies>
      </plugin>
    

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 2018-07-04
      • 2018-10-06
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多