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

Mockito 2 - Mocking final classes under androidTest does not work #1173

Closed
jolsys opened this issue Aug 23, 2017 · 13 comments
Closed

Mockito 2 - Mocking final classes under androidTest does not work #1173

jolsys opened this issue Aug 23, 2017 · 13 comments
Labels

Comments

@jolsys
Copy link

jolsys commented Aug 23, 2017

Hello,

Default behaviour of mockito-core and mockito-android does not contain functionalities to mock final classes:
testCompile "org.mockito:mockito-core:+"
androidTestCompile "org.mockito:mockito-android:+"

To mock final classes under test/ package we can replace:
testCompile "org.mockito:mockito-core:+" with testCompile "org.mockito:mockito-inline:+"

To mock finall classes under androidTest we should create a file:
/mockito-extensions/org.mockito.plugins.MockMaker containing the value mock-maker-inline.
This soultion does not work. I still receive the same exception that final classes canot be mocked.

Cheers!

@raphw
Copy link
Member

raphw commented Aug 23, 2017

The final mock maker does not work on Android.

@jolsys
Copy link
Author

jolsys commented Aug 24, 2017

thanks! Do you have it on the roadmap to make it work?

@raphw
Copy link
Member

raphw commented Aug 26, 2017

There is no real possibility to make this work in Android at the moment as it lacks the instrumentation API on top of which we are operating. The Android VM is not a standard VM and only implements a subset of the Java specification. As long as Google does not choose to extend its JVM, I am afraid that this feature will not work.

@raphw raphw closed this as completed Aug 26, 2017
@tmurakami
Copy link
Contributor

@jolsys, if you would like to mock your final classes on Android VM, you can use the DexOpener library.

@jolsys
Copy link
Author

jolsys commented Aug 29, 2017

Hey, Thanks I try to apply dexopener + mockito-android but I fail, I create a new project follow instructions in readme, but i receive:

Test running failed: Unable to find instrumentation info for: ComponentInfo{com.tdd.nilesh.opendex1.test/android.support.test.runner.AndroidJUnitRunner}

My gradle files:
build.gradle.txt
build.gradle.Project.txt

@tmurakami
Copy link
Contributor

If you would like to run tests with adb, run "adb shell am instrument -w com.tdd.nilesh.opendex1.test/com.github.tmurakami.dexopener.DexOpenerAndroidJUnitRunner".
To run tests on Android Studio, specify the correct runner in the Run/Debug Configurations dialog.

@jolsys
Copy link
Author

jolsys commented Aug 30, 2017

Unfortunately Android Studio 2.3.1. does not allow to set InstrumentationRunner via Run/Debug Configuration it should be smart enough to pick it up on its own but unfortunately it is not.

@tmurakami
Copy link
Contributor

Could you remove your test settings from Run/Debug Configurations and rerun your tests?

@jolsys
Copy link
Author

jolsys commented Aug 30, 2017

Regarding running tests from Android Studio runner:
Yes, I did and with the existing project it did not work, so I created a new one and it started working :)

Regarding terminal:
When I run the command "adb shell am instrument -w com.tdd.nilesh.opendex1.test/com.github.tmurakami.dexopener.DexOpenerAndroidJUnitRunner", I receive:
error.txt
I assume I need to add a AndroidManifest.xml under androidTest and specify the:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package=[your test package]> <!--suppress AndroidDomInspection --> <instrumentation> <!--suppress AndroidElementNotAllowed --> <meta-data android:name="notPackage" android:value="net.bytebuddy" /> </instrumentation> </manifest>

@tmurakami
Copy link
Contributor

tmurakami commented Aug 30, 2017

Running an independent test with using notPackage will fail due to the Android Testing Support Library specification.
The error was resolved at Android Testing Support Library 1.0. Could you test with that version?

@jolsys
Copy link
Author

jolsys commented Aug 30, 2017

I run the test against Android Testing Support Library 1.0 and it works fine.
I can run it from android studio or from a command line.
Thank you for your help!

@jolsys
Copy link
Author

jolsys commented Aug 31, 2017

When I created a sample app, everything started working fine, now I try to apply dexopener to the already existing project, I do the same steps but I get this response:
BuildConfig exception.txt
Not sure what I can do wit this exception.

@tmurakami
Copy link
Contributor

You need to put your own AndroidJUnitRunner into your androidTest directory and use DexOpener.Builder.
Please refer to README.md and example.

@bric3 bric3 added the android label Mar 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants