Using Androidx test dependencies in Robolectric instrumentation tests - android

I am having an issue with testImplementation and androidTestImplementationin build.gradle. I know that the former is used for unit tests while the latter for instrumentation tests.
Now, the problem is that Roboletric recommends using AndroidX ActivityScenario for launching activities and this class lives in androidx.test:rules. So to make ActivityScenario visible for unit tests, I will have to use testImplementation to declare androidx.test:rules dependency in build.gradle (e.g testImplementation 'androidx.test:rules:1.1.1').
What this implies is that androidx.test:rules classes won't be available for instrumentation tests.
Please, how do I resolve this issue? Do I have to declare a separate androidTestImplementation 'androidx.test:rules:1.1.1' for instrumentaion tests while keeping testImplementation 'androidx.test:rules:1.1.1' for unit tests?

Related

How to test Activity/Fragment with ViewBinding generated classes?

I recently started fixing a test class for fragment, and the fragment got migrated to ViewBinding form Butterknife implementation.
The test started failing since then.
Now I have option to use Roboelectric with PowerMock / Roboelectric with Mockito.
So far I have tried multiple ways. None of them actually worked. Moreover I understood, version matching of these dependencies is a "thing".
I tried to refer these approaches: https://github.com/robolectric/robolectric/issues/5598 but no luck.
So here what I am using:
testImplementation 'org.robolectric:robolectric:4.6-alpha-1'
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0'
testImplementation 'org.mockito:mockito-inline:4.6.1'
testImplementation "org.powermock:powermock-module-junit4:2.0.2"
testImplementation "org.powermock:powermock-api-mockito2:2.0.2"
Any suggestion/help will be helpful regarding this.
Thanks in advance.

How to use PowerMock with Mockito3.x?

I am trying to use PowerMock for the first time. Currently, my app is using Mockito 3.x. I tried to setup PowerMock with following test dependencies:
testImplementation 'org.powermock:powermock-api-mockito:1.6.6'
testImplementation 'org.powermock:powermock-module-junit4-rule-agent:2.0.0'
testImplementation 'org.powermock:powermock-module-junit4-rule:2.0.0'
testImplementation 'org.powermock:powermock-module-junit4:2.0.0'
But, this results in failing test cases with the following error:
java.lang.NoClassDefFoundError: org/mockito/MockitoAnnotations$Mock
After reading the PowerMock doc, it seems that PowerMockito is not compatible with Mockito 3.x. If this is not the case, please help me to set up the PowerMock with Mockito 3.x
According to #1049, mockito 3.x is now supported by Powermock.

In a Kotlin unit test, why does Android Studio show Mockito as unresolved reference EVEN THOUGH it compiles & tests pass?

Pretty weird problem: Mockito deps look broken even though they actually work.
Why does this happen and how to fix it?
The tooltip over those errors says "unresolved reference".
Some details:
These are unit tests (under app/src/test), not Android instrumentation tests.
This only happens when the test is Kotlin. In equivalent Java test, Mockito deps show up just fine in Android Studio.
To reiterate, even though Mockito stuff is shown in red, it still works: the test compiles and passes, both on the command line and in Android Studio.
In build.gradle, under dependencies:
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.19.0'
// ...
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50"
(Also I have a file named org.mockito.plugins.MockMaker containing mock-maker-inline, so that Mockito works with final Kotlin classes.)
Edit: This shouldn't be relevant, but for Android instrumentation tests, there's also this. (I had some problems updating to Mockito 2 earlier, so sticking with 1.10.19 there.)
// Here keeping older Mockito for now
androidTestImplementation 'org.mockito:mockito-core:1.10.19'
// dexmaker needed for Mockito to work in androidTest
androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'
Using Android Studio 3.5.3
What I've tried
Re-sync the project (build.gradle)
Run clean Gradle task inside AS
Close the project and open it again
Quit Android Studio completely and open it again
Update
I think this is related to several versions of Mockito being present in the project.
When I Cmd-click on the names shown in red, it takes me to sources in mockito-core-1.9.5-sources.jar; but when I Cmd-click on ArgumentMatchers or MockitoJUnitRunner, it takes me to mockito-core-2.19.0-sources.jar, the correct one.
I investigated with app:dependencies, and the only reference to Mockito 1.9.5 is through dexmaker-mockito.
+--- com.google.dexmaker:dexmaker-mockito:1.2
| +--- com.google.dexmaker:dexmaker:1.2
| \--- org.mockito:mockito-core:1.9.5 -> 1.10.19 (*)
But as dexmaker dependency is only for androidTest it shouldn't affect anything under test, right...? 🤔
Edit: indeed, the androidTest deps somehow confused Android Studio; commenting out all mockito and dexmaker deps in androidTestImplementation removed the faulty error highlights (but as mentioned, the different version was used for a reason).
In the end, Android Studio update fixed this.
Oh well, this was a bug in Android Studio: updating to latest version (3.6.2) fixed it.
The androidTestImplementation dependencies were conflicting with testImplementation ones; see updated question for details.

Cannot resolve ActivityTestRule after upgrading dependencies. Unable to import ActivityTestRule

I have written instrumentation tests which was working fine, but now getting error cannot resolve ActivityTestRule error after upgrading dependencies to
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
I am using android support version 27.1.1
It is working fine with dependencies
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
In the most recent update of the Testing Support Library (2018-04-24), the "rules" dependency was apparently removed from the espresso-core. I'm not sure why this was done, but the release notes say this about the change:
Fixed espresso-core POM file to not pull in "rules" dependency, instead have espresso-intents POM pull it. This should be a NoOp change for developers since espresso-intents cannot be used without espresso-core.
You can fix this problem by adding the following dependency to app/build.gradle:
androidTestImplementation 'com.android.support.test:rules:1.0.2'
AndroidX Test includes another API,ActivityScenario that is currently in beta. This API works in a variety of testing environments and provides thread safety within the tests that use it. Consider using ActivityScenarioRule or ActivityScenario instead.
import androidx.test.ext.junit.rules.ActivityScenarioRule
ActivityScenarioRule(YourActivity::class.java)

AndroidJUnit4 not found after Gradle update on AndroidStudio 3.1.1

I had been running my unit test so far without any problems until we decide to update the gradle version from 3.1 to 4.6 and the gradle plugging from 2.3.3 to 3.5. After this update the project run without any problem and even the unit tests runs fine but not the ones that need to import AndroidJUnit4 because it can't resolve the dependence any more.
On the build.gradle we are importing:
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test:rules:1.0.1'
Any idea?
Thanks in advance
androidTestImplementation and the runner are used for UI tests. You seem to not be explicitly importing JUnit:
testImplementation "junit:junit:4.12"

Categories

Resources