Spring Boot Applicatio
Service Layer Testing
By Ramesh Fadatare (Java Guides)
Spring Boot Application
Service Layer Testing
Controller Service Repository
DB
Layer Layer Layer
Mockito
Service Layer Testin
1.Create EmployeeService with saveEmployee method
2.Quick Recap of Mockito basics (before writing Unit tests)
3.Unit test for EmployeeService saveEmployee method
4.Using @Mock and @InjectMocks annotations to mock the object
5.Unit test for saveEmployee method which throws Exception
6.Unit test for EmployeeService getAllEmployees method - Positive Scenario
7.Unit test for EmployeeService getAllEmployees method - Negative Scenario
8.Unit test for EmployeeService getEmployeeById method
9.Unit test for EmployeeService updateEmployee method
10.Unit test for EmployeeService deleteEmployee method
g
JUnit tests in BDD Style
Syntax Example
@Test
public void given_when_then() {
// given - precondition or setup
// when - action or the behaviour we’re testing
// then - verify the output
}