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

feat: Controller 테스트에서 Application Context 재활용하도록 변경 (#78) #80

Merged
merged 1 commit into from
Jan 6, 2024
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package kr.galaxyhub.sc.api.v1.auth.oauth2

import com.ninjasquad.springmockk.MockkBean
import io.kotest.core.spec.style.DescribeSpec
import io.mockk.every
import kr.galaxyhub.sc.api.support.ENUM
import kr.galaxyhub.sc.api.support.STRING
Expand All @@ -12,19 +10,15 @@ import kr.galaxyhub.sc.api.support.pathMeans
import kr.galaxyhub.sc.api.support.type
import kr.galaxyhub.sc.auth.application.OAuth2FacadeService
import kr.galaxyhub.sc.auth.application.dto.LoginResponse
import kr.galaxyhub.sc.config.spec.ControllerTestSpec
import kr.galaxyhub.sc.member.domain.SocialType
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc

@WebMvcTest(OAuth2ControllerV1::class)
@AutoConfigureRestDocs
class OAuth2ControllerV1Test(
private val mockMvc: MockMvc,
@MockkBean
private val oAuth2FacadeService: OAuth2FacadeService,
) : DescribeSpec({
) : ControllerTestSpec({

describe("POST /api/v1/auth/oauth2/login") {
context("유효한 요청이 전달되면") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
package kr.galaxyhub.sc.api.v1.crawler

import com.fasterxml.jackson.databind.ObjectMapper
import com.ninjasquad.springmockk.MockkBean
import io.kotest.core.spec.style.DescribeSpec
import io.mockk.every
import java.util.UUID
import kr.galaxyhub.sc.api.support.STRING
import kr.galaxyhub.sc.api.support.andDocument
import kr.galaxyhub.sc.api.support.docPost
import kr.galaxyhub.sc.api.support.type
import kr.galaxyhub.sc.api.v1.crawler.dto.NewsCrawlingRequest
import kr.galaxyhub.sc.config.spec.ControllerTestSpec
import kr.galaxyhub.sc.crawler.application.CrawlerCommandService
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc

@WebMvcTest(CrawlerControllerV1::class)
@AutoConfigureRestDocs
class CrawlerControllerV1Test(
private val mockMvc: MockMvc,
private val objectMapper: ObjectMapper,
@MockkBean
private val crawlerCommandService: CrawlerCommandService,
) : DescribeSpec({
) : ControllerTestSpec({

describe("POST /api/v1/crawler") {
context("유요한 요청이 전달되면") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package kr.galaxyhub.sc.api.v1.news

import com.fasterxml.jackson.databind.ObjectMapper
import com.ninjasquad.springmockk.MockkBean
import io.kotest.core.spec.style.DescribeSpec
import io.mockk.every
import io.mockk.justRun
import java.time.LocalDateTime
Expand All @@ -23,27 +21,22 @@ import kr.galaxyhub.sc.api.support.pathMeans
import kr.galaxyhub.sc.api.support.type
import kr.galaxyhub.sc.api.v1.news.dto.NewsCreateRequest
import kr.galaxyhub.sc.api.v1.news.dto.NewsUpdateRequest
import kr.galaxyhub.sc.config.spec.ControllerTestSpec
import kr.galaxyhub.sc.news.application.NewsCommandService
import kr.galaxyhub.sc.news.application.NewsQueryService
import kr.galaxyhub.sc.news.application.dto.NewsDetailResponse
import kr.galaxyhub.sc.news.application.dto.NewsResponse
import kr.galaxyhub.sc.news.domain.Language
import kr.galaxyhub.sc.news.domain.NewsType
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc

@WebMvcTest(NewsControllerV1::class)
@AutoConfigureRestDocs
class NewsControllerV1Test(
private val mockMvc: MockMvc,
private val objectMapper: ObjectMapper,
@MockkBean
private val newsQueryService: NewsQueryService,
@MockkBean
private val newsCommandService: NewsCommandService,
) : DescribeSpec({
) : ControllerTestSpec({

describe("GET /api/v1/news/{newsId}") {
context("유효한 요청이 전달되면") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package kr.galaxyhub.sc.api.v1.translation

import com.fasterxml.jackson.databind.ObjectMapper
import com.ninjasquad.springmockk.MockkBean
import io.kotest.core.spec.style.DescribeSpec
import io.mockk.every
import java.util.UUID
import kr.galaxyhub.sc.api.support.ENUM
Expand All @@ -13,27 +11,22 @@ import kr.galaxyhub.sc.api.support.docPost
import kr.galaxyhub.sc.api.support.pathMeans
import kr.galaxyhub.sc.api.support.type
import kr.galaxyhub.sc.api.v1.translation.dto.TranslationRequest
import kr.galaxyhub.sc.config.spec.ControllerTestSpec
import kr.galaxyhub.sc.news.domain.Language
import kr.galaxyhub.sc.translation.application.TranslationCommandService
import kr.galaxyhub.sc.translation.application.TranslationQueryService
import kr.galaxyhub.sc.translation.application.dto.TranslationResponse
import kr.galaxyhub.sc.translation.domain.TranslationStatus
import kr.galaxyhub.sc.translation.domain.TranslatorProvider
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc

@WebMvcTest(TranslationControllerV1::class)
@AutoConfigureRestDocs
class TranslationControllerV1Test(
private val mockMvc: MockMvc,
private val objectMapper: ObjectMapper,
@MockkBean
private val translationCommandService: TranslationCommandService,
@MockkBean
private val translationQueryService: TranslationQueryService,
) : DescribeSpec({
) : ControllerTestSpec({

describe("POST /api/v1/translation/{newsId}") {
context("유효한 요청이 전달되면") {
Expand Down
35 changes: 35 additions & 0 deletions src/test/kotlin/kr/galaxyhub/sc/config/spec/ControllerTestSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kr.galaxyhub.sc.config.spec

import io.kotest.core.spec.style.DescribeSpec
import io.kotest.core.test.TestCase
import io.kotest.core.test.TestResult
import io.mockk.clearAllMocks
import io.mockk.mockkClass
import org.junit.platform.commons.util.ClassFilter
import org.junit.platform.commons.util.ReflectionUtils
import org.springframework.beans.factory.config.BeanFactoryPostProcessor
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.context.annotation.Import
import org.springframework.stereotype.Service

@Import(MockServiceBeanFactoryPostProcessor::class)
@WebMvcTest
@AutoConfigureRestDocs
abstract class ControllerTestSpec(body: DescribeSpec.() -> Unit = {}) : DescribeSpec(body) {

override suspend fun afterEach(testCase: TestCase, result: TestResult) {
clearAllMocks()
}
}

class MockServiceBeanFactoryPostProcessor : BeanFactoryPostProcessor {

override fun postProcessBeanFactory(beanFactory: ConfigurableListableBeanFactory) {
val classFilter = ClassFilter.of { it.isAnnotationPresent(Service::class.java) }
ReflectionUtils.findAllClassesInPackage("kr.galaxyhub.sc", classFilter).forEach {
beanFactory.registerSingleton(it.simpleName, mockkClass(it.kotlin))
}
}
}