-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [chore] : 테스트 관련 의존성 추가 * [test] : 테스트 컨테이너 설정 파일 추가 * [test] : repository 테스트용 추상 클래스 추가 * [test] : controller 통합 테스트용 추상클래스 추가 * [rename] : test support 클래스들 파일 경로 수정 * [test] : testFixture 디렉토리 추가 * [chore] : 빌드를 위한 dockerfile 추가
- Loading branch information
Showing
7 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/test/java/com/dnd/gongmuin/common/fixture/QuestionPostFixture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
src/test/java/com/dnd/gongmuin/common/support/ApiTestSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/test/java/com/dnd/gongmuin/common/support/DataJpaTestSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
51 changes: 51 additions & 0 deletions
51
src/test/java/com/dnd/gongmuin/common/support/TestContainerSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
src/test/java/com/dnd/gongmuin/config/TestAuditingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |