diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..cb33e1bb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM openjdk:17-jdk +ARG JAR_FILE=build/libs/*.jar +COPY ${JAR_FILE} app.jar +ENTRYPOINT ["java", "-jar", "app.jar"] \ No newline at end of file diff --git a/build.gradle b/build.gradle index affc2ab3..b04f8529 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/src/test/java/com/dnd/gongmuin/common/fixture/QuestionPostFixture.java b/src/test/java/com/dnd/gongmuin/common/fixture/QuestionPostFixture.java new file mode 100644 index 00000000..056c7a60 --- /dev/null +++ b/src/test/java/com/dnd/gongmuin/common/fixture/QuestionPostFixture.java @@ -0,0 +1,9 @@ +package com.dnd.gongmuin.common.fixture; + +import static lombok.AccessLevel.*; + +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = PRIVATE) +public class QuestionPostFixture { +} diff --git a/src/test/java/com/dnd/gongmuin/common/support/ApiTestSupport.java b/src/test/java/com/dnd/gongmuin/common/support/ApiTestSupport.java new file mode 100644 index 00000000..108cb5e7 --- /dev/null +++ b/src/test/java/com/dnd/gongmuin/common/support/ApiTestSupport.java @@ -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); + } + +} diff --git a/src/test/java/com/dnd/gongmuin/common/support/DataJpaTestSupport.java b/src/test/java/com/dnd/gongmuin/common/support/DataJpaTestSupport.java new file mode 100644 index 00000000..c381c15c --- /dev/null +++ b/src/test/java/com/dnd/gongmuin/common/support/DataJpaTestSupport.java @@ -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 { +} diff --git a/src/test/java/com/dnd/gongmuin/common/support/TestContainerSupport.java b/src/test/java/com/dnd/gongmuin/common/support/TestContainerSupport.java new file mode 100644 index 00000000..32843318 --- /dev/null +++ b/src/test/java/com/dnd/gongmuin/common/support/TestContainerSupport.java @@ -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); + } + +} diff --git a/src/test/java/com/dnd/gongmuin/config/TestAuditingConfig.java b/src/test/java/com/dnd/gongmuin/config/TestAuditingConfig.java new file mode 100644 index 00000000..26fcf0d1 --- /dev/null +++ b/src/test/java/com/dnd/gongmuin/config/TestAuditingConfig.java @@ -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 { +}