JMeter and JUnit Integration
In this tutorial, we will learn to integrate JMeter and JUnit. This JMeter and JUnit integration help in load testing of customized Java methods – the JUnit tests. Integrating JUnit in JMeter helps in finding the time taken by individual tests with the applied load using various JMeter capabilities. In this post, we will create a sample JUnit test and then configure it to run in JMeter.
Content
Steps to integrate JMeter with JUnit
- Create a JUnit Test Project
- Create Jar for the JUnit Project
- Put Jar in JMeter’s lib/junit directory
- 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.
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
package SamplePackage;
import org.junit.*;
public class SampleJUnitTest {
//Constructors
public sampleJUnitTest() {
}
public sampleJUnitTest(String test) {
this();
}
@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 the Export button.
- Inside Java, click on the ‘JAR file’.
- Select your Project and check the resources. Also, provide the path for the generated Jar file.
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
- 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 a test.
- Add Listeners and run the test.
This concludes running JUnit tests in the JMeter tutorial. Please comment below if you have any queries. Check out the complete JMeter tutorial below.