From 2611e8eac6a85e18cfd1d834e3472bf5a9637a47 Mon Sep 17 00:00:00 2001 From: michaelin007 Date: Thu, 4 Jan 2024 15:02:36 +0000 Subject: [PATCH] https://jira.baeldung.com/browse/BAEL-7375 --- persistence-modules/spring-boot-persistence/pom.xml | 1 + .../tests/SpringBootInitialLoadIntegrationTest.java | 12 ++++++------ .../src/test/resources/delete_employees_data.sql | 1 + 3 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 persistence-modules/spring-boot-persistence/src/test/resources/delete_employees_data.sql diff --git a/persistence-modules/spring-boot-persistence/pom.xml b/persistence-modules/spring-boot-persistence/pom.xml index 123e21ce59..39edc01170 100644 --- a/persistence-modules/spring-boot-persistence/pom.xml +++ b/persistence-modules/spring-boot-persistence/pom.xml @@ -74,6 +74,7 @@ 2.0.1.Final 8.2.0 com.baeldung.boot.Application + 3.2.0 \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/springbootinitialload/tests/SpringBootInitialLoadIntegrationTest.java b/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/springbootinitialload/tests/SpringBootInitialLoadIntegrationTest.java index fa2807777a..6a7eb6a680 100644 --- a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/springbootinitialload/tests/SpringBootInitialLoadIntegrationTest.java +++ b/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/springbootinitialload/tests/SpringBootInitialLoadIntegrationTest.java @@ -1,6 +1,7 @@ package com.baeldung.springbootinitialload.tests; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.*; import org.junit.Test; import org.junit.runner.RunWith; @@ -16,7 +17,8 @@ import com.baeldung.boot.repository.EmployeeRepository; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) -@Sql({"/employees_schema.sql", "/import_employees.sql"}) +@Sql(scripts = {"/employees_schema.sql", "/import_employees.sql"}, executionPhase = BEFORE_TEST_CLASS) +@Sql(scripts = {"/delete_employees_data.sql"}, executionPhase = AFTER_TEST_CLASS) public class SpringBootInitialLoadIntegrationTest { @Autowired @@ -24,14 +26,12 @@ public class SpringBootInitialLoadIntegrationTest { @Test public void testLoadDataForTestClass() { - assertEquals(3, employeeRepository.findAll() - .size()); + assertEquals(3, employeeRepository.findAll().size()); } @Test - @Sql(scripts = {"/import_senior_employees.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = TransactionMode.ISOLATED)) + @Sql(scripts = {"/import_senior_employees.sql"}, executionPhase = BEFORE_TEST_METHOD ,config = @SqlConfig(encoding = "utf-8", transactionMode = TransactionMode.ISOLATED)) public void testLoadDataForTestCase() { - assertEquals(5, employeeRepository.findAll() - .size()); + assertEquals(5, employeeRepository.findAll().size()); } } diff --git a/persistence-modules/spring-boot-persistence/src/test/resources/delete_employees_data.sql b/persistence-modules/spring-boot-persistence/src/test/resources/delete_employees_data.sql new file mode 100644 index 0000000000..212c6bda28 --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/test/resources/delete_employees_data.sql @@ -0,0 +1 @@ +DROP TABLE EMPLOYEES;