본문 바로가기
Spring

[Spring] Mockito Inline Mock 경고

by Nhahan 2025. 2. 22.

(경고 메시지)

더보기
Mockito is currently self-attaching to enable the inline-mock-maker. This will no longer work in future releases of the JDK. Please add Mockito as an agent to your build what is described in Mockito's documentation: https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#0.3
WARNING: A Java agent has been loaded dynamically (/path/to/byte-buddy-agent.jar)
WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
WARNING: Dynamic loading of agents will be disallowed by default in a future release
(Mockito Inline Mock 경고)

(참고: https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#0.3)

 

 

Mockito는 테스트 코드에서 목(mock) 객체를 생성할 때, 인라인 목 기능을 사용한다.

인라인 목이 무엇이냐하면, final 클래스나 메서드까지도 목(mock)할 수 있도록 해주는 기능이다.

이를 위해 Byte Buddy 라이브러리를 사용하여 런타임 시 바이트코드를 조작한다.

그런데 자바 21 부터 보안상의 이유로 라이브러리가 동적으로 이러한 행위를 하는 것을 제한한다.

친절하게도 지금까지는 허용해주지만 추후에는 이를 제한할 것이라고 경고하는 것이다.

WARNING: Dynamic loading of agents will be disallowed by default in a future release

 

 

해결 방법

버전은 알아서 잘 해주도록 하자. 아래는 5.14.0 버전을 사용.

1. build.gradle.kts (Kotlin)

val mockitoAgent = configurations.create("mockitoAgent")
dependencies {
    testImplementation("org.mockito:mockito-core:5.14.0")
    mockitoAgent("org.mockito:mockito-core:5.14.0") {
        isTransitive = false
    }
}
tasks.test {
    jvmArgs("-javaagent:${mockitoAgent.asPath}")
}

 

 

2. build.gradle (Groovy)

configurations {
    mockitoAgent
}
dependencies {
    testImplementation 'org.mockito:mockito-core:5.14.0'
    mockitoAgent 'org.mockito:mockito-core:5.14.0' {
        transitive = false
    }
}
test {
    jvmArgs += "-javaagent:${configurations.mockitoAgent.asPath}"
}

 


 

ps1

Gradle Version Catalog을 사용한다면 좀 더 멋지게 처리가 가능하다.
더보기
# libs.versions.toml
[versions]
mockito = "5.14.0"

[libraries]
mockito = { module = "org.mockito:mockito-core", version.ref = "mockito" }

 

 

// build.gradle.kts
val mockitoAgent = configurations.create("mockitoAgent")
dependencies {
    testImplementation(libs.findLibrary("mockito").get())
    mockitoAgent(libs.findLibrary("mockito").get()) {
        isTransitive = false
    }
}
tasks.test {
    jvmArgs("-javaagent:${mockitoAgent.asPath}")
}

(이런 식으로)

 

 

ps2

Kotlin은 기본적으로 final class이므로 이 경고를 볼 확률이 높다.

 

 

ps3

메이븐은... 주로 레거시에서 쓰니 볼 일 없을 듯?

 

 

 

반응형

댓글