Junit test plan in jmeter

In this chapter we are going to explain how to use JMeter in combination with JUnit. This provides very strong mechanisms for executing functional tests from a load and performance point of view.
  • First of all you need to create a Junit test like the following one:
01public class JMeterTest
02{
03 
04    public JMeterTest()
05    {
06        // just for testing purposes
07        System.out.println( "calling the constructor" );
08    }
09 
10    @Before
11    public void setUp() throws Exception
12    {
13        // just for testing purposes
14        System.out.println( "calling setup" );
15    }
16 
17    @After
18    public void tearDown() throws Exception
19    {
20        // just for testing purposes
21        System.out.println( "calling tearDown" );
22 
23    }
24 
25    @Test
26    public void testMe()
27    {
28        forint i = 0; i < 30; i++ )
29        {
30            // just asserting randomly in order to check what happens in each case
31            int randomNumber = (int)( Math.random() * 100 );
32            System.out.println( randomNumber );
33            assertEquals( 0, randomNumber % 2 );
34        }
35    }
36}
This test mainly contains a couple of assertions in order to check how JMeter can use them, it is not the purpose of this tutorial to explain unit testing or JUnit in detail.
  • Add test to JUnit directory
Package the unit test in a jar file and copy it into the directory jmeter/lib/junit. Ensure that the jar file contains the .class bytecode with the tests that you want JMeter to execute. At the end of this article you can find a java project with a pom.xml that you can directly use to generate a jar file that can be used by JMeter. You just need to compile the code:
1mvn compile
Or you can export your project as JAR file including the bytecode and generated files directly in Eclipse or your favorite IDE.
  • Add a JUnit sampler request
Junit sampler request
Create a test plan as we saw before and add a sampler of the type JUnit request: Add Sampler->Junit Request and configure it in order to execute the methods that you want from the test that you just created above.
In our case we are going to execute JUnit 4 based methods, but you can also do this using JUnit 3 by selecting the check box for this purpose.
Junit Sampler Request Configuration
  • Add a listener to view results as we did in previous chapters
  • Run the test plan
  • View the results
Here we can see the results of the JUnit test that we have just created:
junit view results
As we can see in the screenshot above, all our tests (1 test, 10 loops) failed. This is the expected behaviour since we wanted our test to fail. Actually the unit test that we just wrote has no functional meaning and it is useless. Its purpose is just to show how to configure JMeter to use these kind of unit tests.
The stuff explained in this chapter may be very helpful to test functional and business logic of application using JMeter with all the benefits that this application provides, it is also very interesting because it makes possible to combine all the Java language advantages with the multithreating capabilites of JMeter.