Mercurial > hg > SpringPlayground
view spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTest.java @ 5:227f3105fedd
Erster Unit Test, der das repository einfach nur mockt
author | Dirk Olmes <dirk.olmes@codedo.de> |
---|---|
date | Thu, 13 Aug 2020 15:58:57 +0200 |
parents | |
children |
line wrap: on
line source
package de.comline.spring.service; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import de.comline.spring.entity.Movie; import de.comline.spring.repository.MovieRepository; import de.comline.spring.service.MovieServiceTest.TestConfig; @SpringBootTest(classes = { MovieService.class, TestConfig.class }) public class MovieServiceTest { @Autowired private MovieService service; @Test @DisplayName("Test with a mock movie repository") void withMockRepository() { String title = "the-movie-title"; Movie movie = service.createMovie(title); assertThat(movie.getTitle(), is(title)); } @Configuration static class TestConfig { @Bean MovieRepository createMockRepository() { MovieRepository mockRepository = mock(MovieRepository.class); when(mockRepository.save(any())).thenAnswer(invocation -> { return invocation.getArgument(0); }); return mockRepository; } } }