Skip to content

JUnit Request

Configure the JMeter JUnit Request samplers: properties, defaults, and practical usage notes for building reliable load tests.

Difficulty
intermediate
Guide type
reference
Estimated read time
4 min read
Last verified version
Verified JMeter 5.6

Part of the Samplers category. Also documented in context in the full Component Reference.

JUnit Request

The current implementation supports standard JUnit convention and extensions. It also includes extensions like oneTimeSetUp and oneTimeTearDown. The sampler works like the Java Request with some differences.

  • rather than use JMeter’s test interface, it scans the jar files for classes extending JUnit’s TestCase class. That includes any class or subclass.
  • JUnit test jar files should be placed in jmeter/lib/junit instead of /lib directory. You can also use the “user.classpath” property to specify where to look for TestCase classes.
  • JUnit sampler does not use name/value pairs for configuration like the Java Request. The sampler assumes setUp and tearDown will configure the test correctly.
  • The sampler measures the elapsed time only for the test method and does not include setUp and tearDown.
  • Each time the test method is called, JMeter will pass the result to the listeners.
  • Support for oneTimeSetUp and oneTimeTearDown is done as a method. Since JMeter is multi-threaded, we cannot call oneTimeSetUp/oneTimeTearDown the same way Maven does it.
  • The sampler reports unexpected exceptions as errors. There are some important differences between standard JUnit test runners and JMeter’s implementation. Rather than make a new instance of the class for each test, JMeter creates 1 instance per sampler and reuses it. This can be changed with checkbox “Create a new instance per sample”.

The current implementation of the sampler will try to create an instance using the string constructor first. If the test class does not declare a string constructor, the sampler will look for an empty constructor. Example below:

Empty Constructor:

public class myTestCase {
public myTestCase() {}
}

String Constructor:

public class myTestCase {
public myTestCase(String text) {
super(text);
}
}

By default, JMeter will provide some default values for the success/failure code and message. Users should define a set of unique success and failure codes and use them uniformly across all tests.

NameRequiredDescription
NameNoDescriptive name for this element that is shown in the tree.
Search for JUnit4 annotationsYesSelect this to search for JUnit4 tests (@Test annotations)
Package filterNoComma separated list of packages to show. Example, org.apache.jmeter,junit.framework.
Class nameYesFully qualified name of the JUnit test class.
Constructor stringNoString pass to the string constructor. If a string is set, the sampler will use the string constructor instead of the empty constructor.
Test methodYesThe method to test.
Success messageNoA descriptive message indicating what success means.
Success codeNoAn unique code indicating the test was successful.
Failure messageNoA descriptive message indicating what failure means.
Failure codeNoAn unique code indicating the test failed.
Error messageNoA description for errors.
Error codeNoSome code for errors. Does not need to be unique.
Do not call setUp and tearDownYesSet the sampler not to call setUp and tearDown. By default, setUp and tearDown should be called. Not calling those methods could affect the test and make it inaccurate. This option should only be used with calling oneTimeSetUp and oneTimeTearDown. If the selected method is oneTimeSetUp or oneTimeTearDown, this option should be checked.
Append assertion errorsYesWhether or not to append assertion errors to the response message.
Append runtime exceptionsYesWhether or not to append runtime exceptions to the response message. Only applies if “Append assertion errors” is not selected.
Create a new Instance per sampleYesWhether or not to create a new JUnit instance for each sample. Defaults to false, meaning JUnit TestCase is created one and reused.

The following JUnit4 annotations are recognised:

@Test : used to find test methods and classes. The “expected” and “timeout” attributes are supported.

@Before : treated the same as setUp() in JUnit3

@After : treated the same as tearDown() in JUnit3

@BeforeClass, @AfterClass : treated as test methods so they can be run independently as required

On this page