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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot inject mocks when using dexmaker-mockito-inline #119

Open
orestislamprousis opened this issue Oct 10, 2018 · 9 comments
Open

Cannot inject mocks when using dexmaker-mockito-inline #119

orestislamprousis opened this issue Oct 10, 2018 · 9 comments

Comments

@orestislamprousis
Copy link

Hi i have an issue using dexmaker-mockito-inline from within android studio.

I want to use it in order to mock some final methods of a class but before doing that i try to run some tests that were previously successfully run on target when i was using mockito-android.

I run my test class with MockitoJUnitRunner and i use mock annotations like @mock and @Injectmocks
and i seem to have issues when initMocks() is called:

The stacktrace is the following:

Started running tests

java.lang.NullPointerException: Attempt to invoke interface method 'boolean org.mockito.plugins.MockMaker$TypeMockability.mockable()' on a null object reference
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:23)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:232)
at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:226)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:64)
at org.mockito.Mockito.mock(Mockito.java:1871)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:36)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)
at org.mockito.internal.configuration.IndependentAnnotationEngine.createMockFor(IndependentAnnotationEngine.java:38)
at org.mockito.internal.configuration.IndependentAnnotationEngine.process(IndependentAnnotationEngine.java:62)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:57)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:41)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:69)
at org.mockito.internal.runners.DefaultInternalRunner$1.withBefores(DefaultInternalRunner.java:39)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:276)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)

Test running failed: Test run failed to complete. Expected 4 tests, received 1

My build.gradle looks like this

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "myAppId"
        minSdkVersion 27
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.19.1'
    implementation project(':my_interface')
}

What could be the cause of it?
Previously there was no issue when i was using

androidTestImplementation 'org.mockito:mockito-core:2.21.0'
androidTestImplementation 'org.mockito:mockito-android:2.21.0'

Thank you in advance

@moltmann
Copy link
Contributor

  1. Can you give the whole logcat for the time from before you started the test to after
  2. My guess that in the logcat of (1) it complains about your application not being marked as android:debuggable or you are not using a Android P platform

@orestislamprousis
Copy link
Author

Hi,

thank you for your reply. Here is the logcat:
logcat.txt

the component i try to test is mentioned as "my_service".

Related to your second point in the previous comment i do run on Android P and i have set my application as debuggable before running the test. From what i can see in logcat there seems to be some ABI incompatibility issue that stroke my attention.

@moltmann
Copy link
Contributor

Can you please provide the whole logcat (via adb logcat), from 5 seconds before to 5 seconds after your test.

@orestislamprousis
Copy link
Author

Here it is
logcat.txt

@moltmann
Copy link
Contributor

What device are you running on? This should not happen on a release version of Android P. Did you update your enumerator to the latest version.

Here is the issue.

�[38;5;196m11-11` 11:37:18.634  6581  6601 E MockMakerMultiplexer: Caused by: java.lang.RuntimeException: Could not initialize inline mock maker.�[0m
�[38;5;196m11-11 11:37:18.634  6581  6601 E MockMakerMultiplexer: �[0m
�[38;5;196m11-11 11:37:18.634  6581  6601 E MockMakerMultiplexer: Release: Android 9 1Device: my-device�[0m
�[38;5;196m11-11 11:37:18.634  6581  6601 E MockMakerMultiplexer: 	at com.android.dx.mockito.inline.InlineDexmakerMockMaker.<init>(InlineDexmakerMockMaker.java:179)�[0m
�[38;5;196m11-11 11:37:18.634  6581  6601 E MockMakerMultiplexer: 	... 40 more�[0m
�[38;5;196m11-11 11:37:18.634  6581  6601 E MockMakerMultiplexer: Caused by: java.lang.IllegalStateException: Cannot find VMDebug#allowHiddenApiReflectionFrom.�[0m
�[38;5;196m11-11 11:37:18.634  6581  6601 E MockMakerMultiplexer: 	at com.android.dx.mockito.inline.InlineDexmakerMockMaker.<clinit>(InlineDexmakerMockMaker.java:132)�[0m
�[38;5;196m11-11 11:37:18.634  6581  6601 E MockMakerMultiplexer: 	at java.lang.Class.classForName(Native Method)�[0m
�[38;5;196m11-11 11:37:18.634  6581  6601 E MockMakerMultiplexer: 	at java.lang.Class.forName(Class.java:453)�[0m
�[38;5;196m11-11 11:37:18.634  6581  6601 E MockMakerMultiplexer: 	at java.lang.Class.forName(Class.java:378)�[0m
�[38;5;196m11-11 11:37:18.634  6581  6601 E MockMakerMultiplexer: 	at com.android.dx.mockito.inline.MockMakerMultiplexer.<clinit>(MockMakerMultiplexer.java:45)�[0m

@orestislamprousis
Copy link
Author

It is a custom proprietary device running Android but unfortunately i cannot disclose something more on that.

Which enumerator do you refer to?

@moltmann
Copy link
Contributor

If you run a custom Android, please make sure that you branch this Android off the final pi-dev branch.

Your Android seems to be missing the change that added this method.

@orestislamprousis
Copy link
Author

It seems i have the appropriate commits in place. Could it be that i use non-SDK API as the commit mentions? But it is a quite simple test i try to run where i try to mock an Intent and a Context. The Context is part of the SDK i guess?

@moltmann
Copy link
Contributor

moltmann commented Oct 18, 2018

I don't think it matters what you mock as we fail to initialize the mockito mockmaker. I recommend to write a test that does nothing else than access VMDebug#allowHiddenApiReflectionFrom via reflection. Once this is possible this problem your mockito problem will probably go away.

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

2 participants