# HG changeset patch # User Dirk Olmes # Date 1597332800 -7200 # Node ID f3f9102ef7c05e5fa2ca53bb6d4ca618873729b5 # Parent 29e2e914c4cdb300133acff8217a43d5c0b8c7f7 Noch ein Test mit der in-memory Datenbank: diesmal wird ein Testdatensatz vorher rein geladen, der dann im Test geholt wird. diff -r 29e2e914c4cd -r f3f9102ef7c0 spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithInMemoryDatabase.java --- a/spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithInMemoryDatabase.java Thu Aug 13 17:17:45 2020 +0200 +++ b/spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithInMemoryDatabase.java Thu Aug 13 17:33:20 2020 +0200 @@ -3,6 +3,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,6 +13,8 @@ import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.jdbc.Sql; import de.comline.spring.entity.Movie; @@ -20,6 +23,7 @@ HibernateJpaAutoConfiguration.class, MovieService.class, }) +@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) @EnableJpaRepositories("de.comline.spring.repository") @EntityScan("de.comline.spring.entity") public class MovieServiceTestWithInMemoryDatabase { @@ -35,4 +39,11 @@ assertThat(movie.getTitle(), is(title)); assertTrue(movie.getId() > 0); } + + @Test + @DisplayName("Fetching from a pre-populated database") + @Sql("/sql/movie.sql") + public void fetchFromDatabase() { + assertThat(service.listAllMovies(), is("Shrek,")); + } } diff -r 29e2e914c4cd -r f3f9102ef7c0 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 Thu Aug 13 17:17:45 2020 +0200 +++ b/spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithMockRepo.java Thu Aug 13 17:33:20 2020 +0200 @@ -5,6 +5,7 @@ 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; import org.junit.jupiter.api.Test; @@ -12,6 +13,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; import de.comline.spring.entity.Movie; import de.comline.spring.repository.MovieRepository; @@ -21,6 +23,7 @@ MovieService.class, TestConfig.class }) +@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) public class MovieServiceTestWithMockRepo { @Autowired private MovieService service; diff -r 29e2e914c4cd -r f3f9102ef7c0 spring-boot-playground/src/test/resources/sql/movie.sql --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spring-boot-playground/src/test/resources/sql/movie.sql Thu Aug 13 17:33:20 2020 +0200 @@ -0,0 +1,1 @@ +insert into MOVIE(ID, TITLE) values (1, 'Shrek');