What are JUnit Annotations?
We have seen what JUnit is, how to configure it, and how to write simple code to test our code. Now we will see the different types of JUnit annotations used.
Example for @Test
Following is the example where we are testing the addNumber method. We can change the value for ‘expectedVal’ other than 25 then we will get the failure result.
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Test;
public class TestClass {
@Test
public void testAdd() {
Add add = new Add();
int a = 20, b = 5;
int expectedVal = 25;
assertEquals(expectedVal, add.addNumber(a, b));
}
}
class Add {
public int addNumber(int a, int b) {
return a + b;
}
}
Example for @Before
In the below example, we will initialize the object for Add class and will assign the value in the code added in the method annotated by @Before. When we run the test case then this createInstance method will be executed first as we can see from the output.
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class TestClass {
Add add;
@Before
public void createInstance() {
System.out.println(“Initiating add values”);
this.add = new Add(20, 2, 3);
}
@Test
public void testAdd() {
int expectedVal = 25;
System.out.println(“Executing test code”);
assertEquals(expectedVal, add.addNumbers());
}
}
class Add {
int arr[];
public Add(int …arr) {
this.arr = arr;
}
public int addNumbers() {
int sum = 0;
for(int i = 0; i <arr.length; i++) {
sum = sum + arr[i];
}
return sum;
}
}
Executing test code
Example for @BeforeClass
We will create the calculator class having addNumbers and subtractNumbers methods. And will have 2 test methods inside TestClass for addition and subtraction. As we can see from the screenshot added both the test methods will be executed successfully. From the Output, we can see display method will be called first before all test cases.
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class TestClass {
Calculator calc;
@BeforeClass
public static void display() {
System.out.println(“Inside @BeforeClass”);
}
@Before
public void createInstance() {
System.out.println(“inside @Before”);
this.calc = new Calculator(20, 2, 3);
}
@Test
public void testAdd() {
int expectedVal = 25;
System.out.println(“Executing addition code”);
assertEquals(expectedVal, calc.addNumbers());
}
@Test
public void testSubtract() {
int expectedVal = 15;
System.out.println(“Executing subtraction code”);
assertEquals(expectedVal, calc.subtractNumbers());
}
}
class Calculator {
int arr[];
public Calculator(int …arr) {
this.arr = arr;
}
public int addNumbers() {
int sum = 0;
for(int i = 0; i <arr.length; i++) {
sum = sum + arr[i];
}
return sum;
}
public int subtractNumbers() {
int value = 0;
for(int i = 0; i <arr.length; i++) {
if(i == 0)
value = arr[i];
else
value = value – arr[i];
}
return value;
}
}
inside @Before
Executing addition code
inside @Before
Executing subtraction code
Example for @After
In the below example once test code is executed we are going to make calculator class object as null. From the output below we can see once test code is executed after method is called automatically.
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import org.junit.After;
import org.junit.Test;
public class TestClass {
Calculator calc;
@After
public void createInstance(){
System.out.println(“inside @After”);
calc = null;
System.out.println(“calc object is: ” + calc);
}
@Test
public void testAdd() {
calc = new Calculator(20, 2, 3);
int expectedVal = 25;
System.out.println(“Executing addition code calc object is: ” + calc);
assertEquals(expectedVal, calc.addNumbers());
}
}
class Calculator {
int arr[];
@Override
public String toString() {
return “Calculator [arr=” + Arrays.toString(arr) + “]”;
}
public Calculator(int …arr) {
this.arr = arr;
}
public int addNumbers() {
int sum = 0;
for(int i = 0; i <arr.length; i++) {
sum = sum + arr[i];
}
return sum;
}
public int subtractNumbers() {
int value = 0;
for(int i = 0; i <arr.length; i++) {
if(i == 0)
value = arr[i];
else
value = value – arr[i];
}
return value;
}
}
inside @After
calc object is: null
Example for @AfterClass
In the calculator class having addNumbers and subtractNumbers methods. We will have 2 test methods inside TestClass for addition and subtraction. As we can see from the screenshot added both the test methods will be executed successfully. From the Output, we can see displayAfter method will be called last after all test cases executed.
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Test;
public class TestClass {
Calculator calc;
@AfterClass
public static void displayAfter() {
System.out.println(“inside @AfterClass”);
}
@After
public void createInstance(){
System.out.println(“inside @After”);
calc = null;
System.out.println(“calc object is: ” + calc);
}
@Test
public void testAdd() {
calc = new Calculator(20, 2, 3);
int expectedVal = 25;
System.out.println(“Executing addition code calc object is: ” + calc);
assertEquals(expectedVal, calc.addNumbers());
}
@Test
public void testSubtract() {
calc = new Calculator(20, 2, 3, 5);
int expectedVal = 10;
System.out.println(“Executing subtraction code calc object is: ” + calc);
assertEquals(expectedVal, calc.subtractNumbers());
}
}
class Calculator {
int arr[];
@Override
public String toString() {
return “Calculator [arr=” + Arrays.toString(arr) + “]”;
}
public Calculator(int …arr) {
this.arr = arr;
}
public int addNumbers() {
int sum = 0;
for(int i = 0; i <arr.length; i++) {
sum = sum + arr[i];
}
return sum;
}
public int subtractNumbers() {
int value = 0;
for(int i = 0; i <arr.length; i++) {
if(i == 0)
value = arr[i];
else
value = value – arr[i];
}
return value;
}
}
inside @After
calc object is: null
Executing subtraction code calc object is: Calculator [arr=[20, 2, 3, 5]]
inside @After
calc object is: null
inside @AfterClass
Example for @Ignore
We will add this @Ignore annotation on the testSubtract method as shown in the below. From the output we can see that only add testAdd case is executed and testSubtract is ignored.
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import org.junit.Ignore;
import org.junit.Test;
public class TestClass {
Calculator calc;
@Test
public void testAdd() {
calc = new Calculator(20, 2, 3);
int expectedVal = 25;
System.out.println(“Executing addition code calc object is: ” + calc);
assertEquals(expectedVal, calc.addNumbers());
}
@Ignore
public void testSubtract() {
calc = new Calculator(20, 2, 3, 5);
int expectedVal = 10;
System.out.println(“Executing subtraction code calc object is: ” + calc);
assertEquals(expectedVal, calc.subtractNumbers());
}
}
class Calculator {
int arr[];
@Override
public String toString() {
return “Calculator [arr=” + Arrays.toString(arr) + “]”;
}
public Calculator(int …arr) {
this.arr = arr;
}
public int addNumbers() {
int sum = 0;
for(int i = 0; i <arr.length; i++) {
sum = sum + arr[i];
}
return sum;
}
public int subtractNumbers() {
int value = 0;
for(int i = 0; i <arr.length; i++) {
if(i == 0)
value = arr[i];
else
value = value – arr[i];
}
return value;
}
}
Example for @Test(timeout=200)
In the following example we will add infinite while took to make the test code take the more time than 200 milliseconds. Once time is > 200 milliseconds our test code will stop executing throwing an Exception as “org.junit.runners.model.TestTimedOutException: test timed out after 200 milliseconds”.
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Test;
public class TestClass {
Calculator calc;
@Test(timeout = 200)
public void testAdd() {
calc = new Calculator(20, 2, 3);
int expectedVal = 25;
System.out.println(“Executing addition code calc object is: ” + calc);
boolean check = true;
while(check) {
// this loop is added to increase test time
}
assertEquals(expectedVal, calc.addNumbers());
}
}
class Calculator {
int arr[];
@Override
public String toString() {
return “Calculator [arr=” + Arrays.toString(arr) + “]”;
}
public Calculator(int …arr) {
this.arr = arr;
}
public int addNumbers() {
int sum = 0;
for(int i = 0; i <arr.length; i++) {
sum = sum + arr[i];
}
return sum;
}
public int subtractNumbers() {
int value = 0;
for(int i = 0; i <arr.length; i++) {
if(i == 0)
value = arr[i];
else
value = value – arr[i];
}
return value;
}
}
Example for @Test(expected=AssertionFailedError.class)
In the below example since Test.txt file is not present false will be returned resulting failure of the test case with AssertionFailedError since expectedVal and returned value is not matching. If we add AssertionFailedError as expected exception then Test case will be success even if file is not present.
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.File;
import org.junit.Test;
import org.opentest4j.AssertionFailedError;
public class TestClass {
@Test(expected=AssertionFailedError.class)
public void testAdd() {
boolean expectedVal = true;
Reader reader = new Reader();
assertEquals(expectedVal, reader.findFile(“Test.txt”));
}
}
class Reader {
public boolean findFile(String fileName) {
File file = new File(fileName);
System.out.println(file.exists());
return file.exists();
}
}
We can add the Test.txt file in our working directory and check the test case again for true case. Since we have added that exception is expected. And in true case that exception is not generated, our test case will fail throwing an error as “java.lang.AssertionError: Expected exception: org.opentest4j.AssertionFailedError” as shown in the below image.