Test driven development for a Java enterprise application

Java enterprise application in spring:


A typical spring web application will have the below layers: -

Developing the micro services, the TDD way: -

Challenges to write test cases: -
We know Microservices are also Web application. So to test how do we get the real environment?

Sol: - Use Mock environment like
Environment
Server
Database

Challenges to write test cases: -
Microservices also follows a layered architecture, how do we unit test the individual layer like below?
Controllers <-> Service Layer <-> Repository Layer

1. How to test controller when service layer is not ready?
Controllers <-> Service Layer <-> Repository Layer
Sol: - We will use annotation '@WebMvcTest' which can work with mocked Service Layer as well.

2. Testing service layer without the repository layer being ready?
Service Layer <-> Repository Layer
Sol: - We will use annotation '@MockBean' which can work with mocked Repository layers as well.

3. What if you don’t have an actual database to test your code?
Repository Layer <-> Database
Sol: - We will use annotation '@DataJpaTest' that helps Configure test DB.



4. Microservices may be interacting with other services how to test that?
Microservice-A <-> Microservice-B
Sol: - By using Spring Cloud Contract to create the Stubs of the services which may or may not yet be fully implemented.

Integration test (of all layers) without having a need for deploying on an actual Server.
Microservice-A
Testing
Simulated Web Server
Deploy