diff --git a/topic6/samples/spring-data-sample/pom.xml b/topic6/samples/spring-data-sample/pom.xml
index 66a3854..10d1416 100644
--- a/topic6/samples/spring-data-sample/pom.xml
+++ b/topic6/samples/spring-data-sample/pom.xml
@@ -1,47 +1,65 @@
- 4.0.0
-
- org.springframework.boot
- spring-boot-starter-parent
- 2.2.2.RELEASE
-
-
- kma.topic6
- spring-data-sample
- 0.0.1-SNAPSHOT
- spring-data-sample
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.2.2.RELEASE
+
+
+ kma.topic6
+ spring-data-sample
+ 0.0.1-SNAPSHOT
+ spring-data-sample
-
- 1.8
-
+
+ 1.8
+
-
-
- org.springframework.boot
- spring-boot-starter-data-jpa
-
+
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+
-
- org.projectlombok
- lombok
- true
-
+
+ org.projectlombok
+ lombok
+ true
+
-
- com.h2database
- h2
-
-
+
+ com.h2database
+ h2
+
-
-
-
- org.springframework.boot
- spring-boot-maven-plugin
-
-
-
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.dbunit
+ dbunit
+ 2.6.0
+ test
+
+
+ com.github.springtestdbunit
+ spring-test-dbunit
+ 1.3.0
+ test
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
diff --git a/topic6/samples/spring-data-sample/src/main/java/kma/topic6/springdatasample/embedded/ApartmentEntity.java b/topic6/samples/spring-data-sample/src/main/java/kma/topic6/springdatasample/embedded/ApartmentEntity.java
index e033061..067a515 100644
--- a/topic6/samples/spring-data-sample/src/main/java/kma/topic6/springdatasample/embedded/ApartmentEntity.java
+++ b/topic6/samples/spring-data-sample/src/main/java/kma/topic6/springdatasample/embedded/ApartmentEntity.java
@@ -33,7 +33,7 @@ public class ApartmentEntity {
@Column(name = "number")
private String number;
- @OneToMany(fetch = FetchType.EAGER, mappedBy = "apartment")
+ @OneToMany(fetch = FetchType.LAZY, mappedBy = "apartment")
private List billings;
}
diff --git a/topic6/samples/spring-data-sample/src/test/java/kma/topic6/springdatasample/AbstractTest.java b/topic6/samples/spring-data-sample/src/test/java/kma/topic6/springdatasample/AbstractTest.java
new file mode 100644
index 0000000..fe51187
--- /dev/null
+++ b/topic6/samples/spring-data-sample/src/test/java/kma/topic6/springdatasample/AbstractTest.java
@@ -0,0 +1,9 @@
+package kma.topic6.springdatasample;
+
+import org.springframework.boot.test.context.SpringBootTest;
+
+@MySpringTestListeners
+@SpringBootTest
+public class AbstractTest {
+
+}
diff --git a/topic6/samples/spring-data-sample/src/test/java/kma/topic6/springdatasample/MySpringTestListeners.java b/topic6/samples/spring-data-sample/src/test/java/kma/topic6/springdatasample/MySpringTestListeners.java
new file mode 100644
index 0000000..f686c60
--- /dev/null
+++ b/topic6/samples/spring-data-sample/src/test/java/kma/topic6/springdatasample/MySpringTestListeners.java
@@ -0,0 +1,35 @@
+package kma.topic6.springdatasample;
+
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener;
+import org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener;
+import org.springframework.test.context.TestExecutionListeners;
+import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener;
+import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
+import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
+import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
+
+import com.github.springtestdbunit.DbUnitTestExecutionListener;
+
+@TestExecutionListeners({
+ DependencyInjectionTestExecutionListener.class,
+ DirtiesContextTestExecutionListener.class,
+ DbUnitTestExecutionListener.class,
+ TransactionalTestExecutionListener.class,
+ MockitoTestExecutionListener.class,
+ ResetMocksTestExecutionListener.class,
+ SqlScriptsTestExecutionListener.class
+})
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Inherited
+public @interface MySpringTestListeners {
+
+}
diff --git a/topic6/samples/spring-data-sample/src/test/java/kma/topic6/springdatasample/UserServiceTest.java b/topic6/samples/spring-data-sample/src/test/java/kma/topic6/springdatasample/UserServiceTest.java
new file mode 100644
index 0000000..a3d6746
--- /dev/null
+++ b/topic6/samples/spring-data-sample/src/test/java/kma/topic6/springdatasample/UserServiceTest.java
@@ -0,0 +1,74 @@
+package kma.topic6.springdatasample;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.jdbc.Sql;
+
+import com.github.springtestdbunit.annotation.DatabaseSetup;
+import com.github.springtestdbunit.annotation.DatabaseTearDown;
+import com.github.springtestdbunit.annotation.ExpectedDatabase;
+import com.github.springtestdbunit.assertion.DatabaseAssertionMode;
+
+@DatabaseSetup("/UserService/init.xml")
+@DatabaseTearDown("/clean-up.xml")
+class UserServiceTest extends AbstractTest {
+
+ @Autowired
+ private UserService service;
+
+// @Test
+ @Sql(value = "/UserService/clean-up.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
+ @Sql("/UserService/init.sql")
+ void shouldSelectUserById() {
+ assertThat(service.getUserById(1))
+ .returns(1, UserEntity::getId)
+ .returns("email1@example.com", UserEntity::getEmail);
+ }
+
+// @Test
+ @Sql("/UserService/init.sql")
+ @Sql(value = "/UserService/clean-up.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
+ void shouldSelectAllUsers() {
+ assertThat(service.findAllUsers())
+ .hasSize(3);
+ }
+
+// @Test
+ @Sql("/UserService/init.sql")
+ @Sql(value = "/UserService/clean-up.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
+ void shouldCreateUser() {
+ service.createUser("new-user", "new-user", "email4@example.com");
+
+ assertThat(service.countUsers()).isEqualTo(4L);
+ }
+
+ @Test
+ void shouldSelectUserById_dbunit() {
+ assertThat(service.getUserById(1))
+ .returns(1, UserEntity::getId)
+ .returns("email123@dummy.com", UserEntity::getEmail);
+ }
+
+ @Test
+ void shouldSelectAllUsers_dbunit() {
+ assertThat(service.findAllUsers())
+ .hasSize(3);
+ }
+
+ @Test
+ @ExpectedDatabase(value = "/UserService/expectedUsersAfterCreateNew.xml", assertionMode = DatabaseAssertionMode.NON_STRICT)
+ void shouldCreateUser_dbunit() {
+ service.createUser("new-user", "new-user", "email4@example.com");
+ }
+
+ @Test
+ @ExpectedDatabase(value = "/UserService/expectedUsersAfterCreateNew.xml", assertionMode = DatabaseAssertionMode.NON_STRICT)
+ void shouldCreateUser_dbunit1() {
+ service.createUser("new-user", "new-user", "email4@example.com");
+ }
+
+
+}
diff --git a/topic6/samples/spring-data-sample/src/test/resources/UserService/clean-up.sql b/topic6/samples/spring-data-sample/src/test/resources/UserService/clean-up.sql
new file mode 100644
index 0000000..0c43117
--- /dev/null
+++ b/topic6/samples/spring-data-sample/src/test/resources/UserService/clean-up.sql
@@ -0,0 +1 @@
+delete from user;
diff --git a/topic6/samples/spring-data-sample/src/test/resources/UserService/expectedUsersAfterCreateNew.xml b/topic6/samples/spring-data-sample/src/test/resources/UserService/expectedUsersAfterCreateNew.xml
new file mode 100644
index 0000000..19e2f09
--- /dev/null
+++ b/topic6/samples/spring-data-sample/src/test/resources/UserService/expectedUsersAfterCreateNew.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
diff --git a/topic6/samples/spring-data-sample/src/test/resources/UserService/init.sql b/topic6/samples/spring-data-sample/src/test/resources/UserService/init.sql
new file mode 100644
index 0000000..c9d920f
--- /dev/null
+++ b/topic6/samples/spring-data-sample/src/test/resources/UserService/init.sql
@@ -0,0 +1,4 @@
+insert into user(first_name, last_name, email)
+values ('name1', 'lname1', 'email1@example.com'),
+ ('name2', 'lname2', 'email2@example.com'),
+ ('name2', 'lname2', 'email2@example.com');
diff --git a/topic6/samples/spring-data-sample/src/test/resources/UserService/init.xml b/topic6/samples/spring-data-sample/src/test/resources/UserService/init.xml
new file mode 100644
index 0000000..7bcf9ba
--- /dev/null
+++ b/topic6/samples/spring-data-sample/src/test/resources/UserService/init.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
diff --git a/topic6/samples/spring-data-sample/src/test/resources/clean-up.xml b/topic6/samples/spring-data-sample/src/test/resources/clean-up.xml
new file mode 100644
index 0000000..0c7343a
--- /dev/null
+++ b/topic6/samples/spring-data-sample/src/test/resources/clean-up.xml
@@ -0,0 +1,3 @@
+
+
+