
Hello everyone,
This writing includes;
*What is unit testing?
*When and why do we use unit testing?
*How to do unit testing?
We are going to find the answers to these questions.
What is Unit Testing?
Unit testing is a software development process where the smallest testable parts of an application are individually tested. Actually, unit test purpose is to validate each unit of the software code performs as expected and less defects. When you develop unit testing code, you have to know your writing code. So, unit testing is done during the development of an application by the developers.
What is a unit?
*Object, method, an individual function etc.
Actually, unit testing is a method to save time. Because skipping unit testing endangers time and money. Unit testing is the base form in the test world.
When and Why do we use Unit Testing?
Unit Testing can also be used as a document. Because; We easily understand unit testing methods name (methods name is description) and what it is doing. We do unit testing to improve analysis and implement refactoring results. We don’t have guaranteed correct software after we do unit testing.
Because, each unit is tested separately .
You can test the code well before the application exists.
Unit Testing Rules
You can test the smallest testable parts.
You can use the Given-When-Then method.
*Given – a context
*When – Some condition
*Then – Except some output.
Example Scenario: You Should be able to successfully register on website.
*Given I am new to the website.
* When I go to the registration form.
*And I complete all the required registration details correctly.
* Then I will be registered on the website.
*And I will be automatically logged in.
The template itself is part of Behaviour-Driven Development (BDD).
Come on let’s write unit testing together.
public class Employee {
private String employeeId;
private String name;
private int overTime;
public Employee(String employeeId, String name, int overTime){
this.employeeId = employeeId;
this.name = name;
this.overTime = overTime;
}
public String getEmployeeId(){
return employeeId;
}
public String getName() {
return name;
}
public int getOverTime() {
return overTime;
}
}
public interface EmployeeService {
void makePayment(String employeeId, int overTime);
}
import java.util.List;
public interface EmployeeDB {
List<Employee> getAllEmployees();
}
import java.util.List;
public class PayRoll {
private EmployeeDB employeeDB;
private EmployeeService employeeService;
public PayRoll(EmployeeDB employeeDB, EmployeeService employeeService){
super();
this.employeeDB = employeeDB;
this.employeeService = employeeService;
}
public int overTimePayment(){
List<Employee> employees = employeeDB.getAllEmployees();
for (Employee employee : employees){
employeeService.makePayment(employee.getEmployeeId(),employee.getOverTime());
}
return employees.size();
}
}
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
class PayRollTest {
private PayRoll payRoll;
private EmployeeDB employeeDB;
private List<Employee> employees;
private EmployeeService employeeService;
/***
* @BeforeEach
* Denotes that the annotated method should be executed before each @Test, @RepeatedTest, @ParameterizedTest,
* or @TestFactory method in the current class; analogous to JUnit 4’s @Before. Such methods are inherited
* unless they are overridden.
*/
@BeforeEach
void setUp() {
employees = new ArrayList<Employee>();
// mock creation
employeeDB = mock(EmployeeDB.class);
employeeService = mock(EmployeeService.class);
//The when section is that behavior that you're specifying.
//Finally the then section describes the changes you expect due to the specified behavior.
when(employeeDB.getAllEmployees()).thenReturn(employees);
payRoll = new PayRoll(employeeDB,employeeService);
}
@Test
void testNoEmployees() {
// Asserts that two objects are equal.
assertEquals(0,payRoll.overTimePayment());
}
@Test
void testSingleEmployees() {
employees.add(new Employee("ID4567","Rabia Yurdakul Telef", 5));
assertEquals(1,payRoll.overTimePayment());
}
@Test
void testOnlyOneInteractionDB(){
payRoll.overTimePayment();
// Was the method called one time?
verify(employeeDB,times(1)).getAllEmployees();
}
@Test
void testEmployeeIsOverTimePaid(){
employees.add(new Employee("ID4567","Rabia Yurdakul Telef",5));
assertEquals(1,payRoll.overTimePayment());
verify(employeeService,times(1)).makePayment("ID4567",5);
}
@Test
void testEmployeeAreOverTimePaid(){
employees.add(new Employee("ID4567", "Rabia Yurdakul Telef",5));
employees.add(new Employee("ID3453","Lisa Simpsons",3));
assertEquals(2,payRoll.overTimePayment());
// Use it to capture argument values for further assertions.
ArgumentCaptor<String> idCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Integer> overTimePaymentCaptor = ArgumentCaptor.forClass(Integer.class);
verify(employeeService, times(2)).makePayment(idCaptor.capture(), overTimePaymentCaptor.capture());
assertEquals("ID4567",idCaptor.getAllValues().get(0));
assertEquals("ID3453",idCaptor.getAllValues().get(1));
assertEquals(5,overTimePaymentCaptor.getAllValues().get(0).intValue());
assertEquals(3,overTimePaymentCaptor.getAllValues().get(1).intValue());
}
@Test
void testInteractionOrder(){
String employeeId = "ID4567";
int overTime = 5;
employees.add(new Employee(employeeId,"Rabia Yurdakul Telef", overTime));
assertEquals(1,payRoll.overTimePayment());
InOrder inOrder = inOrder(employeeDB, employeeService);
inOrder.verify(employeeDB).getAllEmployees();
inOrder.verify(employeeService).makePayment(employeeId,overTime);
}
}
See Github project