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

Advertisements

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.

http://alokranjan.freehostia.com/codes/TCP%20Server.zip

Steps:
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.

tcp1

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.

tcp2

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

tcp3
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.

tcp4

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.

ftp11

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

ftp2

You can connect to this file from another system of your network (if only firewall is disabled) through command prompt (ftp 172.26.3.206).Once 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).

ftp3

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.

ftp4

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

ftp5

FAILURE MESSAGES:

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)