# HG changeset patch # User Dirk Olmes # Date 1597667966 -7200 # Node ID 4594ff529ab1260c50496a4375da5cc08760b3dd # Parent a633d0623278ae40ccb8d2d5ffb2223dfd1da4aa Noch ein Test ganz ohne Spring hinzugefuegt. diff -r a633d0623278 -r 4594ff529ab1 spring-boot-playground/src/main/java/de/comline/spring/service/MovieService.java --- a/spring-boot-playground/src/main/java/de/comline/spring/service/MovieService.java Fri Aug 14 08:13:57 2020 +0200 +++ b/spring-boot-playground/src/main/java/de/comline/spring/service/MovieService.java Mon Aug 17 14:39:26 2020 +0200 @@ -11,7 +11,7 @@ @Service public class MovieService { @Autowired - private MovieRepository repository; + protected MovieRepository repository; @Transactional public Movie createMovie(String title) { diff -r a633d0623278 -r 4594ff529ab1 spring-boot-playground/src/test/java/de/comline/spring/service/MovieRepositoryTestWithoutSpring.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spring-boot-playground/src/test/java/de/comline/spring/service/MovieRepositoryTestWithoutSpring.java Mon Aug 17 14:39:26 2020 +0200 @@ -0,0 +1,23 @@ +package de.comline.spring.service; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import de.comline.spring.entity.Movie; + +public class MovieRepositoryTestWithoutSpring { + @Test + @DisplayName("Test with hand-wired service and repository, without Spring") + public void serviceTestWithoutSpring() { + MovieService service = new MovieService(); + service.repository = RepositoryMocker.createMockRepository(); + + String title = "the-movie-title"; + + Movie movie = service.createMovie(title); + assertThat(movie.getTitle(), is(title)); + } +} diff -r a633d0623278 -r 4594ff529ab1 spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithMockRepo.java --- a/spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithMockRepo.java Fri Aug 14 08:13:57 2020 +0200 +++ b/spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithMockRepo.java Mon Aug 17 14:39:26 2020 +0200 @@ -2,9 +2,6 @@ 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 static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD; import org.junit.jupiter.api.DisplayName; @@ -41,11 +38,7 @@ static class TestConfig { @Bean MovieRepository createMockRepository() { - MovieRepository mockRepository = mock(MovieRepository.class); - when(mockRepository.save(any())).thenAnswer(invocation -> { - return invocation.getArgument(0); - }); - return mockRepository; + return RepositoryMocker.createMockRepository(); } } } diff -r a633d0623278 -r 4594ff529ab1 spring-boot-playground/src/test/java/de/comline/spring/service/RepositoryMocker.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spring-boot-playground/src/test/java/de/comline/spring/service/RepositoryMocker.java Mon Aug 17 14:39:26 2020 +0200 @@ -0,0 +1,17 @@ +package de.comline.spring.service; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import de.comline.spring.repository.MovieRepository; + +class RepositoryMocker { + public static MovieRepository createMockRepository() { + MovieRepository mockRepository = mock(MovieRepository.class); + when(mockRepository.save(any())).thenAnswer(invocation -> { + return invocation.getArgument(0); + }); + return mockRepository; + } +}