You are on page 1of 4

Junit Sampler Tutorial

This tutorial attempts to explain the basic design, functionality and usage of the new Junit
Sampler for Jmeter. The sampler was introduced in 2.1.2 release of Jmeter. Earlier releases
do not have the sampler.
Design
The current implementation supports standard Junit convention and extensions, lie
oneTimeSet!p and oneTimeTear"own. #ther features can be added on re$uest. The
sampler wors lie the JavaSampler with some differences.
1. rather than use Jmeter%s test interface, it scans the &ar files for classes extending &unit%s
Test'ase class. This means any class or subclass.
2. Junit test &ar files are copied to &meter(lib(&unit instead of &meter(lib
). Junit sampler does not use name(value pairs for configuration. The sampler assumes
set!p and tear"own will configure the test correctly. *ote+ set!p and tear"own
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
set!p and tear"own.
-. Each time the test method is called, Jmeter will pass the result to the listeners.
.. Support for oneTimeSet!p and oneTimeTear"own is done as a method. Since Jmeter is
multi/threaded, we cannot call oneTimeSet!p(oneTimeTear"own the same way maven
does it.
0. The sampler reports unexpected exceptions as errors.
Functionality
1ere is a description of the functionalty.
Name 2 name for the sample. This is the same as all &meter samplers.
Package Filter 2 provides a way to filter the classes by pacage name.
Classname 2 the name of the class to test. The sampler will scan the &ar files in
&meter(lib(ext and &meter(lib(&unit for classes extending &unit%s Test'ase.
Constructor String 2 a string to pass to the string constructor of the test class.
Test Method 2 the name of the method to test in the sampler.
Success message 2 a descriptive message indicating what success means.
Success code 2 an uni$ue code indicating the test was successful.
Failure message 2 a descriptive message indicating what failure means.
Failure code 2 an uni$ue code indicating the test failed
Error message 2 a description for errors
Error code 2 some code for errors. "oes not need to be uni$ue
Do not call setUp and tearDown 2 set the sampler not to call set!p and tear"own. 3y
default, set!p and tear"own should be called. *ot calling those methods could affect the
test and mae it inaccurate. This option should be used with caution. 4f the selected method
is oneTimeSet!p or oneTimeTear"own, this option should be checed.
Append assertion error 2 3y default, the sampler will not append the assert failures to the
failure message. To see the message in the result tree, chec the option.
Append runtime exception 2 3y default, the sampler will not append the exceptions to
the failure message. To see the stactrace, chec the option
The current implementation of the sampler will try to create an instance using the string
constructor first. 4f the test class does not declare a string constructor, the sampler will loo
for an empty constructor. Example below+
Empty Constructor:
public class myTestCase {
public myTestCase() {}
}
String Constructor:
public class myTestCase {
public myTestCase(String text) {
super(text);
}
}
3y default, Jmeter will provide some default values for the success(failure code and message.
!sers should define a set of uni$ue success and failure codes and use them uniformly across
all tests.
Usage
1ere is a short step/by/step.
1. write your &unit test and &ar the classes
2. copy and paste the &ar files into &meter(lib(&unit directory
). start &meter
,. select 5test plan6
-. right clic add /7 thread group
.. select 5thread group6
0. right clic add /7 sampler /7 &unit re$uest
8. enter 5my unit test6 in the name
9. enter the pacage of your &unit test
1:.select the class you want to test
11.select a method to test
12.enter 5test successful6 in success message
1).enter 51:::6 in success code
1,.enter 5test failed6 in failure message
1-.enter 5:::16 in failure code
1..select the thread group
10.right clic add /7 listener /7view results tree
#ne benefit of the Junit sampler is it allows the user to select any method from a variety of
unit tests to create a test plan. This should reduce the amount of code an user needs to write
to create a variety of test scenarios. ;rom a basic set of test methods, different se$uences
and tests can be created using Jmeter%s <!4.
;or example+
Test Plan1
TestCase1.testImportCustomer
TestCase2.testUpate!anomCustomer
TestCase1.testSelect1""
TestCase2.testUpate#rer
TestCase1.testSelect1"""
TestPlan2
TestCase1.testImportCustomer
TestCase1.testSelect1""
TestCase1.testSelect1"""
TestCase2.test$1""Customers
General Guidelines
1ere are some general guidelines for writing Junit tests so they wor well with Jmeter. Since
Jmeter runs multi/threaded, it is important to eep certain things in mind.
1. =rite the set!p and tear"own methods so they are thread safe. This generally means
avoid using static memebers.
2. >ae the test methods discrete units of wor and not long se$uences of actions. 3y
eeping the test method to a descrete operation, it maes it easier to combine test
methods to create new test plans.
). ?void maing test methods depend on each other. Since Jmeter allows arbitrary
se$uencing of test methods, the runtime behavior is different than the default Junit
behavior.
,. 4f a test method is configurable, be careful about where the properties are stored. @eading
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 oneTimeSet!p and oneTimeTear"own.
.. 4f 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.

You might also like