Skip to content

Commit

Permalink
[#140] test: 테스트코드 속도 개선
Browse files Browse the repository at this point in the history
 - @WebMvcTest → 하나의 Context를 생성하여 사용하도록 동일한 의존성을 가지는 추상클래서 생성 및 상속
  • Loading branch information
JuHyun419 committed Jun 28, 2022
1 parent d7705f8 commit 19e4783
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/test/kotlin/com/yapp/web2/util/AbstractControllerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.yapp.web2.util

import com.ninjasquad.springmockk.MockkBean
import com.yapp.web2.config.S3Uploader
import com.yapp.web2.domain.ControllerTestUtil
import com.yapp.web2.domain.account.controller.AccountController
import com.yapp.web2.domain.account.service.AccountService
import com.yapp.web2.domain.folder.controller.FolderController
import com.yapp.web2.domain.folder.service.FolderService
import com.yapp.web2.security.jwt.JwtProvider
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.test.context.TestPropertySource
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.context.WebApplicationContext
import org.springframework.web.filter.CharacterEncodingFilter
import java.nio.charset.StandardCharsets

@WebMvcTest(
value = [AccountController::class, FolderController::class],
excludeAutoConfiguration = [SecurityAutoConfiguration::class]
)
@AutoConfigureMockMvc(addFilters = false)
@TestPropertySource(properties = ["extension.version=4.0.3"])
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
abstract class AbstractControllerTest {

@Autowired
internal lateinit var mockMvc: MockMvc

@MockkBean
internal lateinit var folderService: FolderService

@MockkBean
private lateinit var jwtProvider: JwtProvider

@Autowired
internal lateinit var context: WebApplicationContext

@MockkBean
internal lateinit var accountService: AccountService

@MockkBean
internal lateinit var s3Uploader: S3Uploader

internal val util = ControllerTestUtil()

@Value("\${extension.version}")
internal lateinit var extensionVersion: String

@BeforeAll
fun setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.addFilters<DefaultMockMvcBuilder>(CharacterEncodingFilter(StandardCharsets.UTF_8.toString(), true))
.alwaysDo<DefaultMockMvcBuilder>(MockMvcResultHandlers.print())
.build()
}

}

0 comments on commit 19e4783

Please sign in to comment.