Skip to content

Mocks are not working on particular devices after update Android SDK from 33 to 34 #3171

@jukov

Description

@jukov

After seeing #3075, I've updated our project to Mockito 5.7.0 (btw I also tried 5.5.0), but even after that mocks are not working anymore on few on my devices. Then I was able to reproduce this issue in sample app which can be found here.

I am trying to run this test:

interface TestInterface {
    fun foo(): Int
}

@RunWith(AndroidJUnit4::class)
class MockitoBugTest {

    @Mock private val testInterface: TestInterface = mock(TestInterface::class.java)

    @Before
    fun setup() {
        Mockito.`when`(testInterface.foo()).thenReturn(42)
    }

    @Test
    fun foo() {
        println("Success")
    }
}

After what I receive the following error:

Stacktrace
java.lang.ExceptionInInitializerError
at org.mockito.internal.debugging.LocationFactory$DefaultLocationFactory.create(LocationFactory.java:48)
at org.mockito.internal.debugging.LocationFactory.create(LocationFactory.java:19)
at org.mockito.internal.debugging.LocationFactory.create(LocationFactory.java:15)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:56)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$DispatcherDefaultingToRealMethod.interceptAbstract(MockMethodInterceptor.java:161)
at info.jukov.mockitobugtest.TestInterface$MockitoMock$KqIWwkGD.foo(Unknown Source:15)
at info.jukov.mockitobugtest.MockitoBugTest.setup(MockitoBugTest.kt:33)
... 32 trimmed
Caused by: java.lang.ClassCastException: java.util.stream.ReferencePipeline$Head cannot be cast to j$.util.stream.Stream
at org.mockito.internal.debugging.LocationImpl$$ExternalSyntheticLambda0.apply(Unknown Source:0)
at java.lang.StackStreamFactory$StackFrameTraverser.consumeFrames(StackStreamFactory.java:601)
at java.lang.StackStreamFactory$AbstractStackWalker.doStackWalk(StackStreamFactory.java:316)
at java.lang.StackStreamFactory$AbstractStackWalker.callStackWalk(StackStreamFactory.java:436)
at java.lang.StackStreamFactory$AbstractStackWalker.beginStackWalk(StackStreamFactory.java:380)
at java.lang.StackStreamFactory$AbstractStackWalker.walk(StackStreamFactory.java:251)
at java.lang.StackWalker.walk(StackWalker.java:503)
at org.mockito.internal.debugging.LocationImpl.stackWalk(LocationImpl.java:134)
at org.mockito.internal.debugging.LocationImpl.framesToSkip(LocationImpl.java:123)
at org.mockito.internal.debugging.LocationImpl.<clinit>(LocationImpl.java:59)
... 40 more

Also I've debugged mock process and found that some problem occurred in SubclassByteBuddyMockMaker.createMock method:

public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
        ...
        try {
            mockInstance = instantiator.newInstance(mockedProxyType);
            MockAccess mockAccess = (MockAccess) mockInstance;
            mockAccess.setMockitoInterceptor(new MockMethodInterceptor(handler, settings)); // Here

            return ensureMockIsAssignableToMockedType(settings, mockInstance);
        } catch (ClassCastException cce) {
           ...
        }

After executing setMockitoInterceptor debugger shows me following error:
Method threw 'java.lang.NoClassDefFoundError' exception. Cannot evaluate info.jukov.mockitobugtest.TestInterface$MockitoMock$tH4GO8Yq.toString()

Additional details:

Tests are not working on:
🚫Google Pixel 6 Pro (Android 14)
🚫Solana Mobile Saga (Android 12)
🚫Emulator Android 14

Tests are working on:
✅OnePlus 9 with Lineage OS (Android 13)
✅Xiaomi Redmi 10 2022 (MIUI 12.5.26, Android 11)
✅Emulator Android 13
✅Emulator Android 12
✅Emulator Android 11

Versions:
Android target SDK: 34
Mockito version: 5.7.0 (also tried 5.5.0)
Kotlin version: 1.9.20 (also tried 1.8.22)
Java version: 11 (also tried 17)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions