JMeter and JUnit Integration

In this tutorial we will see how to integrate JMeter and JUnit. This JMeter and JUnit integration helps in load testing of customized Java methods. Integrating JUnit in JMeter helps in finding the time taken by individual tests with applied load using various JMeter capabilities. In this post, we will create a sample JUnit test and then configure it to run in JMeter.

Steps to integrate JMeter with JUnit

  1. Create a JUnit Test Project
  2. Create Jar for the JUnit Project
  3. Put Jar in JMeter's lib/junit directory
  4. Run JUnit tests in JMeter

Creating a JUnit Test Project

Here, we will create sample Java project having JUnit annotations. It contains a test class sampleJUnitTest.java having to test methods.JUnit with JMeter
For the demo, we have two JUnit Tests in sampleJUnitTest.java - sampleTestPassing and sampleTestFailing. The Test sampleTestPassing gets passed when run and the Test sampleTestFailing is explicitly failed using Assert.fail() command.

SampleJUnitTest.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package SamplePackage;

import org.junit.*;

public class SampleJUnitTest {
 
 @Test
    public void sampleTestPassing() {       
        System.out.println("Running JUnit Sample Test");
    } 
 
 @Test
    public void sampleTestFailing() {       
        Assert.fail();
  System.out.println("Failing JUnit Sample Test");
    }

}


Creating Jar for the JUnit Project

Now we will create a Jar of the above JUnit project. In eclipse, jar can be created easily using Export functionality. Follow the below screenshots for the Jar creation steps- 

Right click on the Project and click on Export button.
JUnit with JMeter
Inside Java, click on 'JAR file'.
JUnit with JMeter
Select your Project and check the resources. Also, provide the path for the generated Jar file.
JUnit with JMeter

Putting Jar in JMeter's lib/junit directory

Next put the generated Jar file in the JMeter's lib/junit directory and restart JMeter.

Running JUnit tests in JMeter

  • First add a "JUnit Request" to a Thread group
  • JUnit with JMeter
  • Check the "Search for JUnit 4 annotations (instead of JUnit3)" checkbox
  • From the "Classname" dropdown select the JUnit Test class created
  • From the "Test Method" dropdown, select the JUnit method/Test you want to load test
  • Likewise multiple JUnit Requests can be added with each request having a Test method- in this example two JUnit requests are added for Passing and failing test
  • JUnit with JMeter
  • Add Listeners and run the test
  • JUnit with JMeter

This concludes running JUnit tests in JMeter tutorial.
- See more at: http://artoftesting.com/performanceTesting/jmeterJunit.html#sthash.dnAv9jS5.dpuf