This tutorial attempts to explain the basic design, functionality and usage of the new JUnit Sampler for JMeter.
The sampler was introduced in version 2.1.2 release of JMeter. Earlier releases do not have the sampler.
27.1 Design
|
The current implementation supports standard JUnit convention and extensions, like oneTimeSetUp
and oneTimeTearDown. Other features can be added on request. The sampler works like the JavaSampler
with some differences.
- Rather than use JMeter's test interface, it scans the jar files for classes extending JUnit's
TestCase class. This means any class or subclass.
- JUnit test jar files are copied to jmeter/lib/junit instead of
jmeter/lib
- JUnit sampler does not use name/value pairs for configuration. The sampler assumes
setUp and tearDown will configure the test correctly.
Note: setUp and tearDown methods must be declared public,
so that JMeter can use it. |
- 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.
|
|
27.4 General Guidelines
|
Here are some general guidelines for writing JUnit tests so they work well with JMeter. Since JMeter
runs multi-threaded, it is important to keep certain things in mind.
- Write the setUp and tearDown methods so they are thread safe. This
generally means avoid using static members.
- Make the test methods discrete units of work and not long sequences of actions. By keeping
the test method to a discrete operation, it makes it easier to combine test methods to create
new test plans.
- Avoid making test methods depend on each other. Since JMeter allows arbitrary sequencing of
test methods, the runtime behavior is different than the default JUnit behavior.
- If a test method is configurable, be careful about where the properties are stored. Reading
the properties from the Jar file is recommended.
- Each sampler creates an instance of the test class, so write your test so the setup happens
in oneTimeSetUp and oneTimeTearDown.
- If you select a class and no methods show up, it means the sampler had a problem creating an
instance of the test class. The best way to debug this is to add some System.out
to your class constructor and see what is happening.
|
|
|