Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 56 additions & 38 deletions topic6/samples/spring-data-sample/pom.xml
Original file line number Diff line number Diff line change
@@ -1,47 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>kma.topic6</groupId>
<artifactId>spring-data-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-data-sample</name>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>kma.topic6</groupId>
<artifactId>spring-data-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-data-sample</name>

<properties>
<java.version>1.8</java.version>
</properties>
<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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<BillingEntity> billings;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kma.topic6.springdatasample;

import org.springframework.boot.test.context.SpringBootTest;

@MySpringTestListeners
@SpringBootTest
public class AbstractTest {

}
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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");
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
delete from user;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<dataset>
<user first_name="name1" last_name="lname1" email="email123@dummy.com" />
<user first_name="name2" last_name="lname2" email="email124@dummy.com" />
<user first_name="name3" last_name="lname3" email="email125@dummy.com" />

<user first_name="new-user" last_name="new-user" email="email4@example.com" />
</dataset>
Original file line number Diff line number Diff line change
@@ -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');
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<dataset>
<user id="1" first_name="name1" last_name="lname1" email="email123@dummy.com" />
<user id="2" first_name="name2" last_name="lname2" email="email124@dummy.com" />
<user id="3" first_name="name3" last_name="lname3" email="email125@dummy.com" />
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<dataset>
<user />
</dataset>