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.

Steps:

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.

SampleTest.java

package mypack;

import junit.framework.TestCase;

public class SampleTest extends TestCase

{

int a=10,b=5;

int c,d,e;

public void testsum()

{

c=a+b;

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

}

public void testmulti()

{

d=a*b;

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

}

public void testdiv()

{

e=a/b;

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.

junit1

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.

junit2

The command prompt will display the output of the method.

junit3

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.

If the page uses Javascript to build up a URL or submit a form, you can use the Proxy Recording facility to create the necessary sampler. If this is not possible, then manual inspection of the code may be needed to determine what the Javascript is doing.

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.

pop2

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

pop3

Method2. (Recording in Jmeter)

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

pop4

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.

pop5

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