Skip to content

Commit

Permalink
[chore] : 테스트 환경 세팅 (#7)
Browse files Browse the repository at this point in the history
* [chore] : 테스트 관련 의존성 추가

* [test] : 테스트 컨테이너 설정 파일 추가

* [test] : repository 테스트용 추상 클래스 추가

* [test] : controller 통합 테스트용 추상클래스 추가

* [rename] : test support 클래스들 파일 경로 수정

* [test] : testFixture 디렉토리 추가

* [chore] : 빌드를 위한 dockerfile 추가
  • Loading branch information
hyun2371 authored Jul 31, 2024
1 parent 04faa6d commit cc8e067
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM openjdk:17-jdk
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
17 changes: 16 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,24 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'

runtimeOnly 'com.mysql:mysql-connector-j'

// lombok 설정
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok:1.18.22'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'

// JUnit
testImplementation "org.junit.jupiter:junit-jupiter:5.8.1"

// TestContainer
testImplementation "org.testcontainers:testcontainers:1.19.5"
testImplementation "org.testcontainers:junit-jupiter:1.19.5"
// mysql 컨테이너
testImplementation "org.testcontainers:mysql:1.19.2"

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dnd.gongmuin.common.fixture;

import static lombok.AccessLevel.*;

import lombok.NoArgsConstructor;

@NoArgsConstructor(access = PRIVATE)
public class QuestionPostFixture {
}
26 changes: 26 additions & 0 deletions src/test/java/com/dnd/gongmuin/common/support/ApiTestSupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.dnd.gongmuin.common.support;

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.test.web.servlet.MockMvc;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

// 컨트롤러 단 통합테스트용
@SpringBootTest
@AutoConfigureMockMvc
public abstract class ApiTestSupport extends TestContainerSupport {

@Autowired
protected MockMvc mockMvc;

@Autowired
protected ObjectMapper objectMapper;

protected String toJson(Object object) throws JsonProcessingException {
return objectMapper.writeValueAsString(object);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.dnd.gongmuin.common.support;

import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;

import com.dnd.gongmuin.config.TestAuditingConfig;

//repository용
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Import(TestAuditingConfig.class)
public abstract class DataJpaTestSupport extends TestContainerSupport {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.dnd.gongmuin.common.support;

import static lombok.AccessLevel.*;

import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;

import lombok.NoArgsConstructor;

@NoArgsConstructor(access = PROTECTED)
public abstract class TestContainerSupport {

private static final String MYSQL_IMAGE = "mysql:8";
private static final String REDIS_IMAGE = "redis:latest";
private static final int MYSQL_PORT = 3306;
private static final int REDIS_PORT = 6379;
private static final JdbcDatabaseContainer<?> MYSQL;
private static final GenericContainer<?> REDIS;

// 싱글톤
static {
MYSQL = new MySQLContainer<>(MYSQL_IMAGE)
.withExposedPorts(MYSQL_PORT)
.withReuse(true);

REDIS = new GenericContainer<>(DockerImageName.parse(REDIS_IMAGE))
.withExposedPorts(REDIS_PORT)
.withReuse(true);

MYSQL.start();
REDIS.start();
}

// 동적으로 DB 속성 할당
@DynamicPropertySource
public static void setUp(DynamicPropertyRegistry registry) {
registry.add("spring.data.redis.host", REDIS::getHost);
registry.add("spring.data.redis.port", ()
-> String.valueOf(REDIS.getMappedPort(REDIS_PORT)));

registry.add("spring.datasource.driver-class-name", MYSQL::getDriverClassName);
registry.add("spring.datasource.url", MYSQL::getJdbcUrl);
registry.add("spring.datasource.username", MYSQL::getUsername);
registry.add("spring.datasource.password", MYSQL::getPassword);
}

}
9 changes: 9 additions & 0 deletions src/test/java/com/dnd/gongmuin/config/TestAuditingConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dnd.gongmuin.config;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@TestConfiguration
@EnableJpaAuditing
public class TestAuditingConfig {
}

0 comments on commit cc8e067

Please sign in to comment.