Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot mocking sealed interface with AGP 8.0 #2975

Open
skaengus2012 opened this issue Apr 16, 2023 · 13 comments
Open

Cannot mocking sealed interface with AGP 8.0 #2975

skaengus2012 opened this issue Apr 16, 2023 · 13 comments

Comments

@skaengus2012
Copy link

skaengus2012 commented Apr 16, 2023

After update AGP 8.0, mockito cannot mock sealed interface. 馃
When sealed keyword was removed, mockito worked very well.

dependecy

AGP : 8.0 
kotlin 1.8.10
org.mockito.kotlin:mockito-kotlin : 4.1.0
org.mockito:mockito-inline : 5.2.0

stacktrace

Mockito cannot mock this class: interface com.nlab.reminder.core.state.EventProcessor.

If you're not sure why you're getting this error, please open an issue on GitHub.


Java               : 17
JVM vendor name    : JetBrains s.r.o.
JVM vendor version : 17.0.6+0-17.0.6b802.4-9586694
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 17.0.6+0-17.0.6b802.4-9586694
JVM info           : mixed mode
OS name            : Mac OS X
OS version         : 13.2.1

Please check the code of the project below:

@kyleholmberg
Copy link

kyleholmberg commented May 3, 2023

I am also having this issue after trying to upgrade to the latest AGP (8.0.1) and Kotlin (1.8.21). All my tests that involve sealed classes are failing to mock said classes. I tried forcing the latest version of Bytebuddy (1.14.4) as I saw some other similar issues that were able to resolve with various versions of Bytebuddy, but this did not work for me.

Any help on this would be appreciated, this is preventing my company from updating to the latest AGP and Kotlin versions.

@MyDogTom
Copy link

MyDogTom commented May 4, 2023

I also experience the same issue. Mocking concrete implementation did work for me. See an example below

sealed interface Response {
    data class Success(data:String): Response
    data class Error(message:String): Response
}

val mockedResponse: Response = mock<Response>() // doesn't work

val mockedResponse: Response = mock<Response.Success>() // does work

@crysxd
Copy link

crysxd commented May 23, 2023

We had the same issue but with AGP 7.4.2, so that doesn't seem to be the cause. We did see the issue after updating to Java 17 and Kotlin 1.8.

The solution was to use specific versions of the sealed class as described by @MyDogTom 馃憤

Java               : 17
JVM vendor name    : JetBrains s.r.o.
JVM vendor version : 17.0.6+0-17.0.6b802.4-9586694
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 17.0.6+0-17.0.6b802.4-9586694
JVM info           : mixed mode
OS name            : Mac OS X
OS version         : 13.3.1


You are seeing this disclaimer because Mockito is configured to create inlined mocks.
You can learn about inline mocks and their limitations under item #39 of the Mockito class javadoc.

Underlying exception : org.mockito.exceptions.base.MockitoException: Unsupported settings with this type ....

@ahoehma
Copy link

ahoehma commented May 30, 2023

I have the same problem with springclass HttpStatusCode

#3024

@DHosseiny
Copy link

same issue here with kotlin and kgp 1.8, agp and gradle 8 and mockito 4.0.0

@Tgo1014
Copy link

Tgo1014 commented Sep 18, 2023

I'm having the same issue. Is there any workaround for this?

@adel-haidar
Copy link

I am having the same problem.

Java : 17
JVM vendor name : Azul Systems, Inc.
JVM vendor version : 17.0.8+7-LTS
JVM name : OpenJDK 64-Bit Server VM
JVM version : 17.0.8+7-LTS
JVM info : mixed mode, sharing
OS name : Mac OS X
OS version : 13.5.2

I have tried creating in the classpath a file /mockito-extensions/org.mockito.plugins.MockMaker containing the value mock-maker-inline (as mentioned in the javadoc under item #39) but, unfortunately, that did not help.

@KarolKsionek
Copy link

Note: Changing the Java compatibility to 11 fixes the issue.

@skaengus2012
Copy link
Author

compatibility

Thank you.
However, the update from Android build to JAVA 17 is recognized as standard, so I think it would be good to support 17 degrees.
The nowInAndroid project is also using Java 17.

@KarolKsionek
Copy link

@skaengus2012 It's not true that Now In Android uses 17.

It uses 17 for build logic https://github.com/android/nowinandroid/blob/main/build-logic/convention/build.gradle.kts

For building Android app and modules it uses Java 11 https://github.com/android/nowinandroid/blob/main/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/KotlinAndroid.kt

@skaengus2012
Copy link
Author

Oh thanks, Good to know

@mahdibohloul
Copy link

I'm having the same problem.

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: interface

If you're not sure why you're getting this error, please open an issue on GitHub.


Java               : 17
JVM vendor name    : Private Build
JVM vendor version : 17.0.9+9-Ubuntu-122.04
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 17.0.9+9-Ubuntu-122.04
JVM info           : mixed mode, sharing
OS name            : Linux
OS version         : 6.6.7-060607-generic

Kotlin: 1.9.22
Gradle: 8.6
Mockito: 5.10.0

@nphausg
Copy link

nphausg commented Apr 11, 2024

I saw that we have a glue for this issue:
#3086 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests