Skip to content
Merged
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
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ dependencies {

// Test Containers
testImplementation 'org.springframework.boot:spring-boot-testcontainers'
testImplementation 'org.testcontainers:mysql:1.19.3'
testImplementation 'org.testcontainers:junit-jupiter:1.19.3'
testImplementation 'org.testcontainers:mysql:1.21.4'
testImplementation 'org.testcontainers:elasticsearch:1.21.4'
testImplementation 'org.testcontainers:junit-jupiter:1.21.4'
testImplementation 'com.redis:testcontainers-redis:2.2.4'

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
import com.techfork.domain.post.repository.PostRepository;
import com.techfork.domain.source.entity.TechBlog;
import com.techfork.domain.source.repository.TechBlogRepository;
import com.techfork.global.configuration.MySQLTestConfig;
import org.junit.jupiter.api.AfterEach;
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.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -31,20 +29,15 @@
/**
* PostController 통합 테스트
* - @SpringBootTest: 전체 애플리케이션 컨텍스트 로드
* - Testcontainers: 실제 MySQL 컨테이너로 통합 테스트
* - MySQLTestConfig.class: 실제 MySQL 컨테이너로 통합 테스트
* - 모든 레이어(Controller, Service, Repository) 통합 테스트
* - MockMvc로 HTTP 요청/응답 테스트
*/
@SpringBootTest
@AutoConfigureMockMvc
@Testcontainers
@Import(MySQLTestConfig.class)
@ActiveProfiles("integrationtest")
class PostControllerIntegrationTest {

@Container
@ServiceConnection
static MySQLContainer<?> mysql = new MySQLContainer<>("mysql:8.0");

@Autowired
private MockMvc mockMvc;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.techfork.global.configuration;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.elasticsearch.ElasticsearchContainer;

@TestConfiguration(proxyBeanMethods = false)
public class ElasticsearchTestConfig {
@Bean
@ServiceConnection
ElasticsearchContainer elasticsearchContainer() {
return new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:8.18.0")
.withEnv("xpack.security.enabled", "false");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.techfork.global.configuration;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.MySQLContainer;

@TestConfiguration(proxyBeanMethods = false)
public class MySQLTestConfig {
@Bean
@ServiceConnection
MySQLContainer<?> mySQLContainer() {
return new MySQLContainer<>("mysql:8.0.36");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.techfork.global.configuration;

import com.redis.testcontainers.RedisContainer;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
public class RedisTestConfig {

@Bean
@ServiceConnection
RedisContainer redisContainer() {
return new RedisContainer(DockerImageName.parse("redis:7.2-alpine"));
}
}
Loading