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.
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.
Filed under: Samplers | 10 Comments »














