Junior — Middle
What components and scenarios do you prefer to cover with automated tests when developing a new microservice?
sobes.tech AI
Answer from AI
When developing a new microservice, I aim to cover the following components and scenarios with automated tests:
- Unit tests for business logic: testing individual methods and classes without external dependencies.
- Integration tests for interaction with databases, external APIs, and other services.
- Controller (API) tests to ensure endpoints handle requests correctly and return expected responses.
- Validation checks for input data and error handling.
Main scenarios:
- Positive cases where all data is correct.
- Negative cases, such as incorrect input data or errors when interacting with external services.
- Boundary conditions and exceptions.
Example of a unit test in Java using JUnit 5:
@Test
void testCalculateDiscount() {
OrderService service = new OrderService();
double discount = service.calculateDiscount(1000);
assertEquals(100, discount);
}