Chathumi Kumarapeli
Posted on October 30, 2021
What is JUnit π€
"JUnit is a simple framework to write repeatable test."
-JUnit Website-
JUnit provides a simple API to test our Java applications. A class or method that we want to test is known as Subject Under Test. To write a test we should create a Java Test Class and use JUnit API to make assumptions on the behaviors of that subject under test. So what JUnit does is verifying whether these assumptions are correct or not.
Architecture of JUnit 5
Basically JUnit is a combination of different modules.
- JUnit Platform: provides an API to launch the test from IDEs, Build Tools, or Console.
- JUnit Jupiter: provides an API to write JUnit tests and extensions.
- JUnit Vintage: provides a test engine implementation which provides backward compatibility if a test is written with JUnit 3 or 4.
- Third party testing frameworks: build their own APIs to write their tests and reuse JUnit platform to launch these tests.
Writing Tests π»
JUnit tests are nothing but methods inside the test class. JUnit identifies a method as a test when you use the annotation @test
on top of the method.
Before getting our hands dirty please head to this GitHub repository where I have added the code required for you to follow up this tutorial. The LearnJUnitStarter folder contains the file set you need to have to get start with this tutorial. Once you clone it to your machine we are good to go π€©
Project Background π§
What I have provided you with is a simple project that contains information - first name, last name, and contact number - of employees. In the file Employee.java you can see all the attributes, getters, setters, and methods related to the Employee class. There we validate that first name, last name, and contact number to be not empty. Plus we throw errors when a contact number does not have 10 digits, or if it contains non-digits, or if it does not start with '0'.
Next we have ManageEmployees.java file. There we have a method to add an employee. But before adding we check whether the employee is already available in the list and also we validate the employee attributes. Now I guess you have an idea on the project that I have given you. π
Write a simple test π©βπ»
Yes, you are going to write your first test method in JUnit π€© We write our tests inside src -> main -> test -> java folder. There create a java class named TestManageEmployees.java. Now we are going to write tests inside it.
public class TestManageEmployees {
}
As I mentioned above, we have a method called addEmployee. Let's write a test to that method. The usual way of naming test methods is adding the word Test in-front of the method name that you are going to test. In this scenario the name of the test method is TestAddEmployee.
public class TestManageEmployees {
public void TestAddEmployee() {
}
}
In JUnit we have annotations to use to say that this method is a test. The annotation we should use here is @Test
.
import org.junit.jupiter.api.Test;
public class TestManageEmployees {
@Test
public void TestAddEmployee() {
}
}
Now we should create an object from ManageEmployees class and use that object to add an employee.
import org.junit.jupiter.api.Test;
public class TestManageEmployees {
@Test
public void TestAddEmployee() {
ManageEmployees employees = new ManageEmployees();
employee.addEmployee("Edward", "Cullen", "0123456789");
}
}
As you can see above, we are to add a new employee named "Edward Cullen". (Yes, I am a Twilight Saga fan π§ββοΈ ) Now we should check whether the employees list is empty or its size is one. Process of verifying our expected output with the test output is know as Assertion. For that JUnit has provided us an Assertions
class.
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class TestManageEmployees {
@Test
public void TestAddEmployee() {
ManageEmployees employees = new ManageEmployees();
employees.addEmployee("Edward", "Cullen", "0123456789");
Assertions.assertFalse( employees.getEmployees().isEmpty() );
Assertions.assertEquals(1, employees.getEmployees().size());
}
}
The line
Assertions.assertFalse( employees.getEmployees().isEmpty() );
we have assertFalse()
method which takes in a Boolean parameter. If the Boolean is true, the method throws an exception and fails the test. So here we check whether the list of employees is null. If we have added the employee accurately you know the list cannot be null. Then we check the size of the list. As we have added only one employee we checks whether its size is 1, using the line,
Assertions.assertEquals(1, employees.getEmployees().size());
Now you can run the test by right clicking on TestAddEmployee and selecting Run 'TestAddEmployee()'.
If you followed up all the steps accurately you should get the following output.
Kudos! Now you know how to write a test in JUnit!!! π₯³ If you are eager to write more complex tests, check the next tutorial as well π
Posted on October 30, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.