Testing Services

A Service with business logic needs to be tested to ensure it functions correctly. In order to do this, a Service like the following can be used:

@RunWith(MockitoJUnitRunner::class)@ActiveProfiles("dev")class MovieServiceTest {    @Mock    lateinit var movieRepository: MovieRepository;    lateinit var movieService: MovieService;

In the preceding test case for MovieService , MovieRepository is annotated with @Mock. During Service, test mocking is done using the Mockito library just to mock repository method invocations and verify the correct invocation. Consider the following code:

    @Before    fun setup() {        movieService = MovieService(movieRepository);    }    

The preceding code is used to create an instance of MovieService with a mocked ...

Get Spring Boot 2.0 Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.