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