changeset 11:f3f9102ef7c0

Noch ein Test mit der in-memory Datenbank: diesmal wird ein Testdatensatz vorher rein geladen, der dann im Test geholt wird.
author Dirk Olmes <dirk.olmes@codedo.de>
date Thu, 13 Aug 2020 17:33:20 +0200
parents 29e2e914c4cd
children 0b52fb868607
files 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/sql/movie.sql
diffstat 3 files changed, 15 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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,"));
+	}
 }
--- 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;
--- /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');