Skip to content

Commit

Permalink
refactor:Testcontainers
Browse files Browse the repository at this point in the history
Consolidate test container setup for repository tests

Merged the PostgreSQL test container configuration into existing repository test classes, removing the redundant ProductRepositoryContainerTest file. This change streamlines the test setup, ensures consistency, and avoids duplicate configurations across repository tests.

#13
  • Loading branch information
k2works committed Dec 27, 2024
1 parent 5714b17 commit 6493bb4
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,35 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest
@Testcontainers
@ActiveProfiles("container")
@DisplayName("部門レポジトリ")
public class DepartmentRepositoryTest {
class DepartmentRepositoryTest {
@Container
private static final PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>(DockerImageName.parse("postgres:15"))
.withUsername("root")
.withPassword("password")
.withDatabaseName("postgres");

@DynamicPropertySource
static void setup(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
}

@Autowired
private DepartmentRepository repository;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
package com.example.sms.service.master.employee;

import com.example.sms.TestDataFactoryImpl;
import com.example.sms.domain.model.master.department.Department;
import com.example.sms.domain.model.master.employee.Employee;
import com.example.sms.service.master.department.DepartmentRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest
@Testcontainers
@ActiveProfiles("container")
@DisplayName("社員レポジトリ")
public class EmployeeRepositoryTest {
class EmployeeRepositoryTest {
@Container
private static final PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>(DockerImageName.parse("postgres:15"))
.withUsername("root")
.withPassword("password")
.withDatabaseName("postgres");

@DynamicPropertySource
static void setup(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
}

@Autowired
private EmployeeRepository repository;

@Autowired
private DepartmentRepository departmentRepository;

@BeforeEach
void setUp() {
repository.deleteAll();
Expand All @@ -27,6 +53,10 @@ private Employee getEmployee() {
return TestDataFactoryImpl.employee("EMP999", "10000", LocalDateTime.of(2021, 1, 1, 0, 0));
}

private Department getDepartment() {
return TestDataFactoryImpl.department("10000", LocalDateTime.of(2021, 1, 1, 0, 0), "部署名");
}

@Test
@DisplayName("社員一覧を取得できる")
void shouldRetrieveAllEmployees() {
Expand All @@ -41,7 +71,9 @@ void shouldRetrieveAllEmployees() {
@DisplayName("社員を登録できる")
void shouldRegisterEmployee() {
Employee employee = getEmployee();
Department department = getDepartment();

departmentRepository.save(department);
repository.save(employee);

Employee actual = repository.findById(employee.getEmpCode()).get();
Expand All @@ -61,6 +93,9 @@ void shouldRegisterEmployee() {
@DisplayName("社員を更新できる")
void shouldUpdateEmployee() {
Employee employee = getEmployee();
Department department = getDepartment();

departmentRepository.save(department);
repository.save(employee);

employee = repository.findById(employee.getEmpCode()).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,36 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import java.util.List;
import java.util.stream.IntStream;

import static org.junit.Assert.assertEquals;

@SpringBootTest
@Testcontainers
@ActiveProfiles("container")
@DisplayName("商品分類レポジトリ")
public class ProductCategoryRepositoryTest {
@Container
private static final PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>(DockerImageName.parse("postgres:15"))
.withUsername("root")
.withPassword("password")
.withDatabaseName("postgres");

@DynamicPropertySource
static void setup(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
}

@Autowired
private ProductCategoryRepository repository;
@Autowired
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,39 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

@SpringBootTest
@Testcontainers
@ActiveProfiles("container")
@DisplayName("商品レポジトリ")
public class ProductRepositoryTest {
@Container
private static final PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>(DockerImageName.parse("postgres:15"))
.withUsername("root")
.withPassword("password")
.withDatabaseName("postgres");

@DynamicPropertySource
static void setup(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
}

@Autowired
private ProductRepository repository;

Expand Down Expand Up @@ -110,6 +133,39 @@ void shouldDeleteProduct() {

assertEquals(0, repository.selectAll().size());
}

@Test
@DisplayName("楽観ロックが正常に動作すること")
void testOptimisticLockingWithThreads() throws InterruptedException {
// Productを新規作成して保存
Product product1 = getProduct("99999999");
repository.save(product1);

// 同じIDのProductをもう一度データベースから取得
repository.findById("99999999").orElseThrow();

// スレッド1でproduct1を更新
Thread thread1 = new Thread(() -> {
Product updatedProduct1 = getProduct("99999999");
repository.save(updatedProduct1);
});

// スレッド2でproduct2を更新し、例外を確認
Thread thread2 = new Thread(() -> {
Product updatedProduct2 = getProduct("99999999");
assertThrows(OptimisticLockingFailureException.class, () -> {
repository.save(updatedProduct2);
});
});

// スレッドを開始
thread1.start();
thread2.start();

// スレッドの終了を待機
thread1.join();
thread2.join();
}
}

@Nested
Expand Down

0 comments on commit 6493bb4

Please sign in to comment.