How To Use JUnit Annotations In Java Project

There are lots of annotations included in Junit. In this article we will describe several annotations that frequently used in java project. If you do not know how to add JUnit framework into your java project, please read How To Integrate JUnit Framework Into Selenium Scripts first.

1. @Test Annotation.
This annotation is used to tell JUnit framework that this annotated
java method is test method that need to run.

 @Test
 public void runJUnitTest()
 {
 System.out.println("This runJunitTest.");
 long int1 = 1;
 long int2 = 2;
 Assert.assertEquals("1 dose not equal to 2", int1, int2);
 }

To execute the method you need right click in your java editor, select “Run as” —> “JUnit test” in popup menu.
run junit test method in java project

2. @Before Annotation.
If you add Before annotation to a java method then it will be executed before execute any test method.

 @Test
 protected void runJUnitTestAnnotation1()
 {
 System.out.println("This is runJUnitTestAnnotation1.");
 }
 
 @Test
 protected void runJUnitTestAnnotation2()
 {
 System.out.println("This is runJUnitTestAnnotation2.");
 }
 
 @Before
 protected void runJUnitBeforeAnnotation()
 {
 System.out.println("Execute before each test function.");
 }

The output for above code is:
Execute before test function.
This is runJUnitTestAnnotation1.
Execute before test function.
This is runJUnitTestAnnotation2.

From above output we can see that the @Before annotated method will execute before each @Test annotated.

3. @BeforeClass Annotation.
If you add @BeforeClass annotation to a java method then it will executes only once before all test method runs. This method must be static in the class. We always add java code to initialize database connection or read properties files in @BeforeClass Annotated java method.

 @Test
 public void runJUnitTestAnnotation1()
 {
 System.out.println("This is runJUnitTestAnnotation1.");
 }
 
 @Test
 public void runJUnitTestAnnotation2()
 {
 System.out.println("This is runJUnitTestAnnotation2.");
 }
 
 @Before
 public void runJUnitBeforeAnnotation()
 {
 System.out.println("Execute before each test function.");
 }
 
 @BeforeClass
 public static void runJUnitBeforeClassAnnotation()
 {
 System.out.println("Execute beforeClass function only once when class is initialized.");
 }

The output for above code:
Execute beforeClass function only once when class is initialized.
Execute before each test function.
This is runJUnitTestAnnotation1.
Execute before each test function.
This is runJUnitTestAnnotation2.

READ :   Introduction To Java File Class

4. @After Annotation.
The java method will be executed right after each test method runs if you add @After annotation to it.

 @Test
 public void runJUnitTestAnnotation1()
 {
 System.out.println("This is runJUnitTestAnnotation1.");
 }
 
 @Test
 public void runJUnitTestAnnotation2()
 {
 System.out.println("This is runJUnitTestAnnotation2.");
 }
 
 @After
 public void runJUnitAfterAnnotation()
 {
 System.out.println("Execute after each test function.");
 }

The output for above code:
This is runJUnitTestAnnotation1.
Execute after each test function.
This is runJUnitTestAnnotation2.
Execute after each test function.

5. @AfterClass Annotation.
Similar to @BeforeClass, @AfterClass annotated method executes only once after all test methods executed. This Annotated method should be static also.

 @Test
 public void runJUnitTestAnnotation1()
 {
 System.out.println("This is runJUnitTestAnnotation1.");
 }
 
 @Test
 public void runJUnitTestAnnotation2()
 {
 System.out.println("This is runJUnitTestAnnotation2.");
 }
 
 @After
 public void runJUnitAfterAnnotation()
 {
 System.out.println("Execute after each testing function.");
 }
 
 @AfterClass
 public static void runJUnitAfterClassAnnotation()
 {
 System.out.println("Execute afterClass function only once when all test methods executed.");
 }

The output for above java code:
This is runJUnitTestAnnotation1.
Execute after each testing function.
This is a runJUnitTestAnnotation2.
Execute after each testing function.
Execute afterClass function only once when all test methods executed.

6. Use JUnit Annotation to parameterized JUnit class.
Parameterized class in JUnit is a java class which can run exact same testing method using several dataset. We can use @RunWith and @Parameters JUnit annotation to implement this.

You can use @Parameters annotation to pass several data as a input class.

@RunWith(Parameterized.class)
public class JUnitTestParameterizedClass {
 //name of the student.
 private String name;
 //age of the student.
 private int age;
 //Class constructor.
 public JUnitTestParameterizedClass(String name,int age){
 this.name=name;
 this.age=age;
 }

//This is the function that will run for JUit.
 @Test
 public void testMethod(){
 System.out.println("Student Name is: "+name +" and student age is: "+age);
 }
 //This is the collection that save array data.
 @Parameters
 public static Collection<Object[]> parameter(){
 //Init a 2*2 dimension data array.
 Object[][] pData=new Object[2][2];
 //Assign pData[0][0]'s value 
 pData[0][0]="Jerry";
 //Assign pData[0][1]'s value
 pData[0][1]=10;
 //Assign pData[1][0]'s value
 pData[1][0]="Jhon";
 //Assign pData[1][1]'s value
 pData[1][1]=15;
 return Arrays.asList(pData);
 }
}

The output of above code:
Student Name is: Jerry and student age is: 10
Student Name is: Jhon and student age is: 15

READ :   Convert Java Array To List

Download “JUnitAnnotationTest.zip” JUnitAnnotationTest.zip – Downloaded 40 times – 1 KB

(Visited 78 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.