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.



12 Responses

  1. Hi Alok,

    i created a jar file with a java class extending the testcase and placed in lib/junit location, but i do not see the class name. can you guide me .

  2. I’ve created a jar file with a junit test case and I’ve put the jar where you’re suggested. I’ve one question – the junit tests a class (e.g. x) which is also in the same jar. Is that OK?

    Now, if i need another package which is called by the class x, should I create another jar and put it inside that lib/junit folder?

    Currently, when I run jmeter, it doesn’t show any output. Here is what I’ve:

    Thread Name: Thread Group 1-1
    Sample Start: 2009-06-28 02:04:58 PDT
    Load time: 0
    Latency: 0
    Size in bytes: 0
    Sample Count: 1
    Error Count: 1
    Response code: 0001
    Response message: Test failed

    Response headers:

    SampleResult fields:
    ContentType: text
    DataEncoding: null

  3. Hi,
    I have started JMeter just 2 days ago.
    I am not able to understand any thing in it.
    Please help me.


  4. @sandy:
    create runnable jar file and keep it in lib/junit folder, if you using eclipse to create jar file.

  5. Hi .. This Article really helped.


  6. Was struggling to get a Junit test going in JMeter.
    Thanks for the sample test and compiling instructions, very helpful.

  7. This is helpful article

  8. Hi All,
    I was able to run the unit test through Jmeter but the output on the command line is showing that it is only running the first test (testsum). In my Thread Group I have “Number of Threads:” set at 3 and Loop Count set at 2. The output in my command line window is as follows:
    The sum of two numbers is: 15
    The sum of two numbers is: 15
    The sum of two numbers is: 15
    The sum of two numbers is: 15
    The sum of two numbers is: 15
    The sum of two numbers is: 15

    It runs correctly when I run the Junit test through my IDE (Eclipse) but not when run through Jmeter.

    Since I am using Junit 4 my code looks a little different than the sample provided in this article but, again it runs correctly when not called from Jmeter.

    Has anyone else experienced this problem or have any ideas on what may be causing it?

    Thank you!

  9. I have tried making another folder name EDD but i dont see EDD in the classname dropdown list.Can anyone help how to get this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: