Employee Management System Service Layer

Learn via video courses
Topics Covered

Overview

We will build our employee management system from the service layer instead of the data layer. This will ensure that we get business logic right in the first place.

Create a Skeleton Project

Use spring initializer to create a skeleton project using web dependency with group id com. scaler and artifact id ems Download the project and import it into the IDE of your choice.

Create a Service Layer

We will create a service interface, EmployeeService, and the corresponding implementation.

  1. Create a package name service under base package com.scaler.ems
  2. Create a service interface.
  1. Implement service interface EmployeeServiceImpl and mark it as a spring service using the @Service annotation.
  1. EmplyeeBO class represents the business object returned by service.

Note: we still need to write actual business code in our service impl. Let's write a test case for employee service.

Testing Service Layer

We will write a JUnit test case to test the service layer. Following the TDD approach, the test may fail initially, but we will correct our business logic to fix the test case.

  1. EmployeeServiceTest to test the employee service

Let's run the test case and see the result. Testing service layer As expected, test cases are failing as we want them to fail.

Implemented code till this point is available in the GitHub repo under tag version_1.0.0.

Enhance Service

We will now enhance our service method to fix the test case. Let's fix one code gap we have.

Service has to fetch data from somewhere. We don't know that yet. Let's call that source EmployeeRepository; therefore, we will create just the interface and inject it into the service.

  1. Create interface EmployeeRepository
  1. Enhance service class with actual business logic.

Code explanation - We have injected an EmployeeRepository in the service without any implementation and written our logic based on interfaces.

That's the advantage of interfaces in java. Using it correctly, we don't need to worry about actual implementation.

  1. Enhance the test case. We now need to enhance our test case to consider employee repositories. The test needs to mock the repository as there is no implementation for it in the code yet.

Let's run the test case and verify the output.

Enhance the test case

As expected, the test cases are green now.

Source code up to this point is available at GitHub under tag version_2.0.0.

Final Version

Let's add the remaining methods in the service and test case. The final code of the service layer with the test case will be.

EmployeeService

EmployeeServiceTest

The final version of the source code is available on GitHub with tag version_3.0.0_FINAL.

Conclusion

In this article, we have

  • Build a service layer for the employee management system.
  • How to write code based on TDD methodology.
  • Service layer is written independently of any downstream and upstream layer.