mockito-kotlinでvalue classがサポートされたらしいのでメモ (original) (raw)
控えめに言って神github.com5.4.0のリリースに含まれているgithub.com
build.gradle.kts
plugins { kotlin("jvm") version "2.0.0" }
group = "com.github.atr0phy" version = "1.0-SNAPSHOT"
repositories { mavenCentral() }
dependencies { testImplementation(kotlin("test")) testImplementation("org.mockito.kotlin:mockito-kotlin:5.4.0") }
tasks.test { useJUnitPlatform() } kotlin { jvmToolchain(17) }
コード
import org.mockito.Mockito.mockStatic import org.mockito.kotlin.any import kotlin.test.Test import kotlin.test.assertEquals
@JvmInline value class SampleValue(val value: String)
object SampleValueRepository { @JvmStatic fun save(sampleValue: SampleValue): SampleValue { return sampleValue } }
private class SampleValueRepositoryTest { @Test fun test() {
val expected = SampleValue("mocked")
val mocked = mockStatic(SampleValueRepository::class.java)
mocked.`when`<SampleValue> { SampleValueRepository.save(any()) }.thenReturn(expected)
val actual = SampleValueRepository.save(SampleValue("call"))
assertEquals(expected, actual)
}
}
結果
5.4.0
Task :checkKotlinGradlePluginConfigurationErrors SKIPPED Task :compileKotlin NO-SOURCE Task :compileJava NO-SOURCE Task :processResources NO-SOURCE Task :classes UP-TO-DATE Task :processTestResources NO-SOURCE Task :compileTestKotlin Task :compileTestJava NO-SOURCE Task :testClasses UP-TO-DATE Task :test BUILD SUCCESSFUL in 3s 2 actionable tasks: 2 executed 23:35:44: ':test --tests "SampleValueTest"' の実行を完了しました。
5.3.1
Task :checkKotlinGradlePluginConfigurationErrors SKIPPED Task :compileKotlin NO-SOURCE Task :compileJava NO-SOURCE Task :processResources NO-SOURCE Task :classes UP-TO-DATE Task :processTestResources NO-SOURCE Task :compileTestKotlin Task :compileTestJava NO-SOURCE Task :testClasses UP-TO-DATE
Misplaced or misused argument matcher detected here:
-> at SampleValueTest.test$lambda$0(SampleValueTest.kt:32)
You cannot use argument matchers outside of verification or stubbing. Examples of correct usage of argument matchers: when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(any()); verify(mock).someMethod(contains("foo"))
This message may appear after an NullPointerException if the last matcher is returning an object like any() but the stubbed method signature expect a primitive argument, in this case, use primitive alternatives. when(mock.get(any())); // bad use, will raise NPE when(mock.get(anyInt())); // correct usage use
Also, this error might show up because you use argument matchers with methods that cannot be mocked. Following methods cannot be stubbed/verified: final/private/equals()/hashCode(). Mocking methods declared on non-public parent classes is not supported.
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced or misused argument matcher detected here:
-> at SampleValueTest.test$lambda$0(SampleValueTest.kt:32)
You cannot use argument matchers outside of verification or stubbing. Examples of correct usage of argument matchers: when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(any()); verify(mock).someMethod(contains("foo"))
This message may appear after an NullPointerException if the last matcher is returning an object like any() but the stubbed method signature expect a primitive argument, in this case, use primitive alternatives. when(mock.get(any())); // bad use, will raise NPE when(mock.get(anyInt())); // correct usage use
Also, this error might show up because you use argument matchers with methods that cannot be mocked. Following methods cannot be stubbed/verified: final/private/equals()/hashCode(). Mocking methods declared on non-public parent classes is not supported.
at SampleValueTest.test(SampleValueTest.kt:22)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
Task :test FAILED SampleValueTest > test() FAILED org.mockito.exceptions.misusing.InvalidUseOfMatchersException at SampleValueTest.kt:22 1 test completed, 1 failed
公式でサポートされたの嬉しい