Linora
Linora

Reputation: 10988

JMeter fails with ClassNotFoundException when run in Maven

I have created a JMeter test file which runs some tests I've written in java

When I run the file in JMeter itself they run fine and the test pass. Now I've configured maven to run the JMX file and when I run the verify goal it executes the JMX file. However now it fails with the following error message:

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

Jmeter.jtl: "Failed to create an instance of the com.mynamespace.tests.product.ProductTest, reasons may be missing both empty constructor and one String constructor or failure to instantiate constructor, check warning messages in jmeter log file"

In order for JMeter to know the class I built a jar and copied it to Jmeter's lib directory. Do I need to do something similar when running the jmeter maven plugin? - surely it should be able to see the Class as the project is built when running the verify goal..

Added to 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>

Upvotes: 1

Views: 2026

Answers (1)

Dmitri T
Dmitri T

Reputation: 168157

You need to modify your pom.xml file to include your jar artifact as a dependency. Something like:

  <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>

References:

Upvotes: 2

Related Questions