본문 바로가기
Blog/TIL

2025-01-21 (화)

by 코젼 2025. 1. 21.
728x90
반응형

Controller WebMvcTest Issue

  • Kotlin 진행
    @WebMvcTest(BusinessUserController::class)  
    class AuthAnnotationTest @Autowired constructor(  
      private val mockMvc: MockMvc  
    ) : DescribeSpec({
  • 런타임 에러 발생 (일부 코드)
  • Kotest 는 테스트 클래스를 인스턴스화 할 때 기본 생성자를 필요로 함.
    • 현재 AuthAnnotationTest 클래스의 생성자는 @Autowired constructor(mockMvc: MockMvc) 형태로 매개변수를 받고 있기 때문에 Kotest 에서 인스턴스를 생성할 수 없음.
      Warning: Kotest autoscan is enabled. This means Kotest will scan the classpath for extensions that are annotated with @AutoScan. 
      To avoid this startup cost, disable autoscan by setting the system property 'kotest.framework.classpath.scanning.autoscan.disable=true'. In 6.0 this value will default to true. 
      For further details see https://kotest.io/docs/next/framework/project-config.html#runtime-detection
      

// --- 핵심 ---
io.kotest.engine.spec.SpecInstantiationException: Could not create instance of class com.mobiai.business.common.annotation.AuthAnnotationTest. Specs must have a public zero-arg constructor
// -------------

at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)

at jdk.proxy1/jdk.proxy1.$Proxy2.stop(Unknown Source)

at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)

## 추가 이슈
- `lateinit var mockMvc: MockMvc` 필드가 @Autowired 되어 있지만, Kotest 실행 시점에 초기화되지 않으면 `UninitializedPropertyAccessException` 예외 발생
    - Kotest의 `DescribeSpec`이 Spring의 `@Autowired` 필드를 초기화하는 것보다 먼저 실행되기 때문

## 해결 방안
- MockMvc 대안으로 Kotest 에서 Mocking 을 직접할 수 있다.
```kotlin
val businessUserController = mockk<BusinessUserController>(relaxed = true)
728x90
반응형

'Blog > TIL' 카테고리의 다른 글

2025-02-03 (월)  (2) 2025.02.03
2025-01-22 (수)  (0) 2025.01.22
2025-01-20 (월)  (1) 2025.01.20
2025-01-16 (목)  (0) 2025.01.16
2025-01-15 (수)  (0) 2025.01.15

댓글