Mercurial > hg > SpringPlayground
changeset 8:7ce4367b50bc
Ein echter Unit Test, der eine in-memory H2 als Teil vom Test startet, das Schema mit liquibase erzeugt und nach dem Test wieder stoppt. Das alles ohne die Web Controller zu starten.
author | Dirk Olmes <dirk.olmes@codedo.de> |
---|---|
date | Thu, 13 Aug 2020 16:51:05 +0200 |
parents | 0c3494137a82 |
children | 5870f5695e43 |
files | spring-boot-playground/src/main/java/de/comline/spring/entity/Movie.java spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTest.java spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithInMemoryDatabase.java spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithMockRepo.java spring-boot-playground/src/test/resources/application.properties |
diffstat | 5 files changed, 91 insertions(+), 48 deletions(-) [+] |
line wrap: on
line diff
--- a/spring-boot-playground/src/main/java/de/comline/spring/entity/Movie.java Thu Aug 13 16:28:20 2020 +0200 +++ b/spring-boot-playground/src/main/java/de/comline/spring/entity/Movie.java Thu Aug 13 16:51:05 2020 +0200 @@ -29,6 +29,10 @@ public Movie(String title) { this.title = title; } + + public long getId() { + return id; + } public String getTitle() { return title;
--- a/spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTest.java Thu Aug 13 16:28:20 2020 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -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; - } - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithInMemoryDatabase.java Thu Aug 13 16:51:05 2020 +0200 @@ -0,0 +1,38 @@ +package de.comline.spring.service; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + +import de.comline.spring.entity.Movie; + +@SpringBootTest(classes = { + DataSourceAutoConfiguration.class, + HibernateJpaAutoConfiguration.class, + MovieService.class, +}) +@EnableJpaRepositories("de.comline.spring.repository") +@EntityScan("de.comline.spring.entity") +public class MovieServiceTestWithInMemoryDatabase { + @Autowired + private MovieService service; + + @Test + @DisplayName("Test with a real movie repository") + void withRealRepository() { + String title = "the-movie-title"; + + Movie movie = service.createMovie(title); + assertThat(movie.getTitle(), is(title)); + assertTrue(movie.getId() > 0); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithMockRepo.java Thu Aug 13 16:51:05 2020 +0200 @@ -0,0 +1,48 @@ +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.MovieServiceTestWithMockRepo.TestConfig; + +@SpringBootTest(classes = { + MovieService.class, + TestConfig.class +}) +public class MovieServiceTestWithMockRepo { + @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; + } + } +}