Challenge: Unit Testing in Spring
Test your understanding of how to effectively use mocks in unit tests to isolate the behavior of the code under test.
We'll cover the following
Problem statement
We have an existing exam management application that calculates the total marks using Spring Boot. The current implementation is basic and lacks thorough unit testing. We need to enhance the unit tests to cover more scenarios, including edge cases, and ensure that the ExamService
class is robust and correctly implemented.
Requirements
You are required to implement the following:
Use Mockito to mock the
ExamRepository
to cover unit tests such as:Normal cases with multiple marks.
Edge cases like empty arrays and single values.
Use assertions to validate not only the results but also the behavior of the mocked repository.
Ensure that the tests cover the full range of possible inputs and are comprehensive.
Try it yourself
Implement the missing parts in the code given below and ensure that the unit tests thoroughly validate the ExamService
implementation.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.