mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.09k stars 198 forks source link

Regression in 5.2.0 POM: missing Mockito dependency for compilation #497

Closed TWiStErRob closed 7 months ago

TWiStErRob commented 7 months ago

Symptom

e: ...kt:0:0 Cannot access class 'org.mockito.stubbing.Stubber'. Check your module classpath for missing or conflicting dependencies
e: ...kt:0:0 Cannot access class 'org.mockito.invocation.InvocationOnMock'. Check your module classpath for missing or conflicting dependencies
e: ...kt:0:0 Unresolved reference: callRealMethod

Repro

interface Factory {
    fun create(): String
}

@Test fun test() {
    val mock = mock<Factory>()
    doAnswer { it.callRealMethod() } // More complex IRL, just simplified, because it doesn't matter.
        .whenever(mock)
        .create()
}

image Note: somehow by simplifying Kotlin knows what InvocationOnMock & callRealMethod is, but it shouldn't, anyway whenever is still broken.

Issue

Compare: https://repo1.maven.org/maven2/org/mockito/kotlin/mockito-kotlin/5.1.0/mockito-kotlin-5.1.0.pom https://repo1.maven.org/maven2/org/mockito/kotlin/mockito-kotlin/5.2.0/mockito-kotlin-5.2.0.pom

Diff

 <dependency>
     <groupId>org.mockito</groupId>
     <artifactId>mockito-core</artifactId>
-    <version>5.3.1</version>
+    <version>5.7.0</version>
-    <scope>compile</scope>
+    <scope>runtime</scope>
 </dependency>

Background

Root cause is this change: https://github.com/mockito/mockito-kotlin/pull/496/files#diff-09728e1a022bed42cb4f8d1d2418235341d939c7f641a015f30648911ec97cc4R29

When upgrading Gradle from compile to implementation @Nynuzoud didn't make an equivalent change. It is a compatible change for most normal projects, but this is a published library, so the equivalent new configuration should be api so that Gradle generates compile in the POM.

Note: you don't observe this, because the tests explicitly depend on transitive dependencies, not via POM.

https://github.com/mockito/mockito-kotlin/blob/5.2.0/tests/build.gradle#L31

TWiStErRob commented 7 months ago

Fix: #498, test: #499