JUnit Sampler Tutorial- JMeter

JUnit sampler provides easy ways to unit test the Java applications. Each Java class that you write will correspond to a JUnit Java class that will test the methods of your class.

For example if you want to test some methods of a class Book you create a class BookTest which extends the JUnit TestCase class and place your test methods in there.( i.e. Create a subclass of TestCase). In this tutorial we create a SampleTest class which has four methods.


1. Create a folder (package) mypack inside lib\junit. Put the SampleTest.java file inside it.

2. Since we are importing TestCase class we need to set the classpath for junit.jar file.


package mypack;

import junit.framework.TestCase;

public class SampleTest extends TestCase


int a=10,b=5;

int c,d,e;

public void testsum()



System.out.println(“The Sum of two numbers is: “+c);


public void testmulti()



System.out.println(“The product of two numbers is: “+d);


public void testdiv()



System.out.println(“The division of two numbers is: “+e);



3. Compile the SampleTest.java file

Cmd:\ Jmeterhome\lib\junit\mypack> javac SampleTest.java

4. Now create a jar file for the package mypack;

Cmd:\Jmeterhome\lib\junit> jar cvf mytest.jar mypack

Here mytest.jar is the name of the jar file that will be generated and mypack is the package name getting jarred.

5. Now since the jar file is inside junit folder Jmeter (Junit Sampler automatically detects the class).

6. Run Jmeter and add a JUnit sampler to the thread group.


Now check the Classname option in the GUI; mypack.SampleTest has been added to the list.Select any method from the test methods available.Run the test and view the result in a listener.


The command prompt will display the output of the method.


POP-UP Recording Limitation in Jmeter

There are generally three kinds of popup-1. Alert box 2. Confirm 3. Prompt. These are Client side scripts/programs generated in the webpage. Since Jmeter is build to load test Servers it can’t record these popups generated during recording a site with Jmeter.

The limitation has also been cited in http://jakarta.apache.org/jmeter/

These are the supported reasons for this:

1. JMeter is not a browser: JMeter is not a browser. As far as web-services and remote services are concerned, JMeter looks like a browser (or rather, multiple browsers); however JMeter does not perform all the actions supported by browsers. In particular, JMeter does not execute the Javascript found in HTML pages. Nor does it render the HTML pages as a browser does (it’s possible to view the response as HTML etc, but the timings are not included in any samples, and only one sample in one thread is ever viewed at a time).

2. The Proxy Server can only record HTTP traffic (any requests to server)

3. Does JMeter process dynamic pages (e.g. Javascript and applets)

JMeter does not process Javascript or applets embedded in HTML pages. JMeter can download the relevant resources (some embedded resources are downloaded automatically if the correct options are set), but it does not process the HTML and execute any Javascript functions.


I have tried verifying this with two methods. In first method I recorded the pop-ups in BADBOY tool and tried to export it to Jmeter. Secondly tried it with Jmeter itself.

Method1: (Exporting from BADBOY)

We record the Alert box and prompt from the below link: http://alokranjan.freehostia.com/pop/popup.htmlpop1

Now we save the script. And when we try to export it the following error comes.


It says items like Messagebox and alert box are not supported in Jmeter.


Method2. (Recording in Jmeter)

Site: http://alokranjan.freehostia.com/pop/popup.html


When we click on the write sth alert box it writes a line (this is a sample line) on the web page.

When we record the site only the page gets recorded and .The alert box and text output doesn’t get recorded.


This confirms that Jmeter can’t handle popups or client side scripts.

Custom Plugins for Jmeter

1. Load Testing RMI server in Jmeter : (New RMI sampler)

2. Better Graphs in Jmeter : (Custom Listener)

3. Enhanced JDBC Sampler for Jmeter

4.Maven JMeter plugin and report generation

4. SMTP Sampler Plugin in Jmeter

5.Testing EJBs with JMeter

6.JMeter Aggregator Plugin

7. Record and playing back RMI calls (RMI Serevr) – Swing Sampler in Jmeter

8.JMeter-groovy-sampler – Runs Groovy Scripts in Jmeter

9. Automate JMeter tests in Maven

10. JMeter to Nagios

TCP Sampler- Jmeter

The TCP Sampler opens a TCP/IP connection to the specified server. It then sends the text, and waits for a response.

You can test any Server working over TCP/IP. But In this example I have tested this sampler with a TCP server made in C#. You can download the TCP server here.


1. Run the TCP server on your system and create a new Test case in Jmeter. The server looks like this before any client connections.


2. Add a TCP sampler to the thread group. Fill the fields as follows-

Server name or IP: Localhost
Check reuse connection
Port number: 13000
Timeout: 200
Check Set Nodelay
Text to send: This is the text i hav sent to tcp server

Since there is no login request for the server we leave the username and password field blank.


3. Add a listener (View Results tree) and run the test.

The sampler result tab gives the response code and load time. The response data shows the data sent by the Server.
You can also check the server whether it has sent the data or not.


Generate the test report in Jmeter


1. ‘Apache-ant’ is the Pre-requisite for generating the report from the jmx file. Apache Ant is a Java-based build tool.
2. Ant-jmeter.jar


1. Create a new variable JAVA_HOME in the environment variable and set the path for the jdk.
2. You can find the Environment variable by right clicking on the computer Icon then properties-> Advance-> environment variable. Then click on the new and give the variable name as well as path also.
3. Likewise create another variable ANT_HOME and set the path for the Apache-ant.
4. Create another variable PATH and set the path for apache-ant up to bin folder as shown in the fig. bellow.

5. Place the ant-jmeter.jar file in side ‘Extras’ folder. Which is located in the path jakarta-jmeter-2.3.2->extras
6. Navigate to the ‘build.xml’ folder of the jmeter which is located in the path jakarta-jmeter-2.3.2->extras->build.xml.
7. Set the classpathref = “d:/jakarta-jmeter-2.3.2/extras/ant-jmeter.jar “
8. Set jmeterhome=”D:/jakarta-jmeter-2.3.2″
9. Set style=”D:/jakarta-jmeter-2.3.2/jmeter-results-detail-report${style_version}.xsl” and save it.
10. Now copy your jmx file and the build.xml file put it in a folder where ever you want to generate the report.
11. Open your command prompt and navigate to the folder where your jmx and build file is located.
12. To run a test and create the output report:
Type the command “ant -Dtest=script”(for eg. I have my test.jms file and the command is ant-Dtest=test)
It will generate an Html file. This can be open in the browser as shown in the fig. bellow.

13. To run a test only:
Type the command “ant -Dtest=script run”
14. To run report on existing test output
Type the command “ant -Dtest=script report”

FTP Sampler – Jmeter

This controller lets you send an FTP “retrieve file” or “upload file” request to an FTP server. If you are going to send multiple requests to the same FTP server, consider using a FTP Request Defaults Configuration Element so you do not have to enter the same information for each FTP Request Generative Controller. When downloading a file, it can be stored on disk (Local File) or in the Response Data, or both.

There are many anonymous ftp sites in the web. Just search in Google as “FTP sites” or “Anonymous FTP sites” and you will get a good list of ftp sites which you can connect to. But if you are unable to connect to one of them due to firewall you can set up your own FTP Server. Download free FTP server from www.goldenftpserver.com/golden-ftp-server.zip and install it with default settings in your system.

Share a file in your server (Open shares->Add).Also share a Folder (enable full control) in your server and name it as Add.


Select a share and click info and it will show the access path to the file.


You can connect to this file from another system of your network (if only firewall is disabled) through command prompt (ftp it is connected you can check the connections to your server.

In this example we are testing a server hosted in the same system.

1. Open a new Test plan in Jmeter and under add a thread group under it. Add two FTP Request Samplers to it. The first sampler is used foe retrieving a file from FTP server and second is for uploading a file to it.

2 .Give server name as localhost (you can also mention the IP).Remote file is given as it is shown in the server and local file is the name under which it will be stored after it gets retrieved. Select get(RETR) option. Enter Login Configurations as required (here anonymous/ anonymous).


Similarly for second sample configure the settings. Here Local file is the file to be uploaded in FTP server (give full path of this file).Remote file is how it will be uploaded into the server (here it will be uploaded in Add folder).Select put (STOR) option.


Now run the test and view results in Results tree listener. If the test is successful the retrieved file is stored in bin folder.



Response code: 550

Response message: 550 Operation not complete

This means that the path where file is to be uploaded is not modifiable. (Should enable full control)

Runtime controller

It fixes the time limit for its children to be run. If you have specified 20 seconds then the samples will be generated for 20 seconds.

Create a new test Plan. Add a thread group to the test plan. Add a Runtime Controller to the Thread group and under the controller add four HTTP Request Element sampler. Name them as 1,2, 3, 4 and put Server name: www.google.co.in and Path:/

Give the Run time as 25 seconds in the controller. Run the script and observe in View listener tree.


In view listener tree select the first sample and under sampler result tab note the sample start time.


Similarly get the start time of the last sample. Their difference gives the Run time we have given.

In this example:-

First sample -Sample Start: 2009-02-05 12:05:06 IST
Last sample- Sample Start: 2009-02-05 12:05:31 IST
The difference comes=00:00:25