Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#102 feat: 각 모듈의 테스트가 정상적으로 동작할 수 있도록 하였니다. #104

Merged
merged 8 commits into from
Jun 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration

@ActiveProfiles("new", "test", "api-repo-local")
@ActiveProfiles("new", "test")
@SpringBootTest(classes = [ApiRepoConfig::class, ObjectMapper::class])
@ContextConfiguration(initializers = [RepoTestContainerInitializer::class])
abstract class JooqTestSpec
14 changes: 13 additions & 1 deletion api-repo/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,16 @@ logging:
org.jooq: DEBUG
org.springframework.jdbc: DEBUG
com.few.api.repo: DEBUG
org.testcontainers: INFO
org.testcontainers: INFO

spring:
datasource:
jdbcUrl: jdbc:mysql://localhost:13306/api?allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
flyway:
locations: classpath:db/migration/entity
sql-migration-suffixes: sql
baseline-on-migrate: true
baseline-version: 0
1 change: 0 additions & 1 deletion api-repo/src/test/resources/mysql-init.d/00_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ GRANT ALL PRIVILEGES ON *.* TO

CREATE
DATABASE api DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE
4 changes: 4 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ dependencies {
implementation("org.springdoc:springdoc-openapi-ui:${DependencyVersion.SPRINGDOC}")
implementation("org.springframework.restdocs:spring-restdocs-webtestclient")
implementation("com.epages:restdocs-api-spec-mockmvc:${DependencyVersion.EPAGES_REST_DOCS_API_SPEC}")

/** test container */
implementation(platform("org.testcontainers:testcontainers-bom:${DependencyVersion.TEST_CONTAINER}"))
testImplementation("org.testcontainers:mysql")
}

plugins {
Expand Down
4 changes: 0 additions & 4 deletions api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@ spring:
prd:
- api-repo-prd
- email-prd
test:
- new
- api-repo-local
- email-local
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.few.api.web.controller

import org.springframework.context.ApplicationContextInitializer
import org.springframework.context.ConfigurableApplicationContext
import org.testcontainers.containers.DockerComposeContainer
import java.io.File

class ControllerTestContainerInitializer : ApplicationContextInitializer<ConfigurableApplicationContext> {
private val log: org.slf4j.Logger =
org.slf4j.LoggerFactory.getLogger(ControllerTestContainerInitializer::class.java)

companion object {
private const val MYSQL = "mysql"
private const val MYSQL_PORT = 3306

private val dockerCompose =
DockerComposeContainer(File("src/test/resources/docker-compose.yml"))
.withExposedService(MYSQL, MYSQL_PORT)
}

override fun initialize(applicationContext: ConfigurableApplicationContext) {
log.debug("===== set up test containers =====")

dockerCompose.start()

log.debug("===== success set up test containers =====")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.restdocs.RestDocumentationExtension
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration

@ActiveProfiles(value = ["test"])
@ActiveProfiles(value = ["test", "new"])
@AutoConfigureRestDocs
@AutoConfigureMockMvc(addFilters = false)
@SpringBootTest(classes = [ApiMain::class])
@ExtendWith(RestDocumentationExtension::class)
@ContextConfiguration(initializers = [ControllerTestContainerInitializer::class])
abstract class ControllerTestSpec {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.http.MediaType
Expand Down Expand Up @@ -136,23 +137,21 @@ class ProblemControllerTest : ControllerTestSpec() {
val uri = UriComponentsBuilder.newInstance()
.path("$BASE_URL/{problemId}").build().toUriString()

val body = objectMapper.writeValueAsString(CheckProblemBody(sub = "sub"))

// set usecase mock
val problemId = 1L
val sub = "제출답"
val body = objectMapper.writeValueAsString(CheckProblemBody(sub = sub))
val useCaseIn = CheckProblemUseCaseIn(problemId, sub = sub)
val useCaseOut = CheckProblemUseCaseOut(
explanation = "ETF는 일반적으로 낮은 운용 비용을 특징으로 합니다.이는 ETF가 보통 지수 추종(passive management) 방식으로 운용되기 때문입니다. 지수를 추종하는 전략은 액티브 매니지먼트(active management)에 비해 관리가 덜 복잡하고, 따라서 비용이 낮습니다.",
answer = "2",
isSolved = true
)
Mockito.`when`(checkProblemUseCase.execute(useCaseIn))
.thenReturn(useCaseOut)
`when`(checkProblemUseCase.execute(useCaseIn)).thenReturn(useCaseOut)

// when
this.webTestClient.post()
.uri(uri, 1)
.uri(uri, problemId)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class SubscriptionControllerTest : ControllerTestSpec() {
// given
val api = "SubscribeWorkBook"
val uri = UriComponentsBuilder.newInstance()
.path("${SubscriptionControllerTest.BASE_URL}/workbooks/{workbookId}/subs").build().toUriString()
.path("$BASE_URL/workbooks/{workbookId}/subs").build().toUriString()

val email = "[email protected]"
val body = objectMapper.writeValueAsString(SubscribeWorkbookRequest(email = email))
Expand All @@ -85,11 +85,11 @@ class SubscriptionControllerTest : ControllerTestSpec() {
val memberId = 1L

val useCaseIn = SubscribeWorkbookUseCaseIn(workbookId = workbookId, email = email, memberId = memberId)
doNothing().`when`(subscribeWorkbookUseCase.execute(useCaseIn))
doNothing().`when`(subscribeWorkbookUseCase).execute(useCaseIn)

// when
this.webTestClient.post()
.uri(uri, 1)
.uri(uri, workbookId)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(body)
Expand Down Expand Up @@ -127,25 +127,23 @@ class SubscriptionControllerTest : ControllerTestSpec() {
.toUriString()

// set usecase mock
val workbookId = 1L
val memberId = 1L
val email = "[email protected]"
val opinion = "취소합니다."
val body = objectMapper.writeValueAsString(
UnsubscribeWorkbookRequest(email = email, opinion = "취소합니다.")
UnsubscribeWorkbookRequest(email = email, opinion = opinion)
)

// set usecase mock
val workbookId = 1L
val memberId = 1L

val useCaseIn = UnsubscribeWorkbookUseCaseIn(
workbookId = workbookId,
email = email,
opinion = "취소합니다."
opinion = opinion
)
doNothing().`when`(unsubscribeWorkbookUseCase.execute(useCaseIn))
doNothing().`when`(unsubscribeWorkbookUseCase).execute(useCaseIn)

// when
this.webTestClient.post()
.uri(uri, 1)
.uri(uri, workbookId)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(body)
Expand Down Expand Up @@ -178,27 +176,26 @@ class SubscriptionControllerTest : ControllerTestSpec() {
// given
val api = "UnsubscribeAll"
val uri = UriComponentsBuilder.newInstance()
.path("${SubscriptionControllerTest.BASE_URL}/subscriptions/unsubs")
.path("$BASE_URL/subscriptions/unsubs")
.build()
.toUriString()

// set usecase mock
val memberId = 1L
val email = "[email protected]"
val opinion = "전체 수신거부합니다."
val body = objectMapper.writeValueAsString(
UnsubscribeWorkbookRequest(email = email, opinion = "전체 수신거부합니다.")
UnsubscribeWorkbookRequest(email = email, opinion = opinion)
)

// set usecase mock
val memberId = 1L
val useCaseIn = UnsubscribeAllUseCaseIn(
email = email,
opinion = "취소합니다."
opinion = opinion
)
doNothing().`when`(unsubscribeAllUseCase.execute(useCaseIn))
doNothing().`when`(unsubscribeAllUseCase).execute(useCaseIn)

// when
this.webTestClient.post()
.uri(uri, 1)
.uri(uri)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(body)
Expand Down
26 changes: 26 additions & 0 deletions api/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
spring:
datasource:
jdbcUrl: jdbc:mysql://localhost:13306/api?allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
flyway:
locations: classpath:db/migration/entity
sql-migration-suffixes: sql
baseline-on-migrate: true
baseline-version: 0
mail:
protocol: smtp
host: smtp.gmail.com
port: 587
username: [email protected]
password: apitest
properties:
mail:
smtp:
auth: true
debug: true
starttls:
enable: true
EnableSSL:
enable: true
14 changes: 14 additions & 0 deletions api/src/test/resources/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3.7'

services:
mysql:
image: mysql/mysql-server:8.0.27
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_ROOT_HOST=%
- TZ=Asia/Seoul
command: [ "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci", "--lower_case_table_names=1", "--max_connections=2048", "--wait_timeout=3600" ]
ports:
- "13306:3306"
volumes:
- ./mysql-init.d:/docker-entrypoint-initdb.d
12 changes: 12 additions & 0 deletions api/src/test/resources/mysql-init.d/00_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE
USER 'few-test-local'@'localhost' IDENTIFIED BY 'few-test-local';
CREATE
USER 'few-test-local'@'%' IDENTIFIED BY 'few-test-local';

GRANT ALL PRIVILEGES ON *.* TO
'few-test-local'@'localhost';
GRANT ALL PRIVILEGES ON *.* TO
'few-test-local'@'%';

CREATE
DATABASE api DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2 changes: 1 addition & 1 deletion batch/src/test/kotlin/com/few/batch/BatchTestSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration

@ActiveProfiles("new", "test", "batch-test", "email-local")
@ActiveProfiles("new", "test")
@SpringBootTest(classes = [BatchConfig::class, ObjectMapper::class])
@ContextConfiguration(initializers = [BatchTestContainerInitializer::class])
abstract class BatchTestSpec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.few.batch.service.article.writer

import com.few.batch.BatchTestSpec
import com.few.email.service.problem.SendArticleEmailService
import com.few.email.service.problem.dto.SendArticleEmailArgs
import com.few.email.service.article.SendArticleEmailService
import com.few.email.service.article.dto.SendArticleEmailArgs
import jooq.jooq_dsl.tables.ArticleIfo
import jooq.jooq_dsl.tables.MappingWorkbookArticle
import jooq.jooq_dsl.tables.Member
Expand Down
15 changes: 15 additions & 0 deletions batch/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,18 @@ spring:
sql-migration-suffixes: sql
baseline-on-migrate: true
baseline-version: 0
mail:
protocol: smtp
host: smtp.gmail.com
port: 587
username: [email protected]
password: batchtest
properties:
mail:
smtp:
auth: true
debug: true
starttls:
enable: true
EnableSSL:
enable: true
1 change: 0 additions & 1 deletion batch/src/test/resources/mysql-init.d/00_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ GRANT ALL PRIVILEGES ON *.* TO

CREATE
DATABASE api DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import com.few.email.config.MailConfig
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles

@ActiveProfiles("test", "email-test")
@ActiveProfiles("test")
@SpringBootTest(classes = [MailConfig::class])
abstract class SendAEmailTestSpec
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ GRANT ALL PRIVILEGES ON *.* TO

CREATE
DATABASE api DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE
Loading