Run Android instrumented tests fail - android

I tried to implement some unit and instrumented tests for my Android application (Java), my unit tests is working fine but instrumented tests are failing:
#RunWith(AndroidJUnit4.class)
#LargeTest
public class ExampleInstrumentedTest {
#Rule
public IntentsTestRule<MainActivity> mActivityRule = new IntentsTestRule<>(MainActivity.class);
#Test
public void checkIfCategoriesIsNotEmpty() {
onView(withId(R.id.header_left_layout)).perform(click());
onView(withId(R.id.list_view)).check(new ViewAssertion() {
#Override
public void check(View view, NoMatchingViewException noViewFoundException) {
ListView list_categories = (ListView) view;
ListAdapter adapter = list_categories.getAdapter();
Assert.assertTrue(adapter.getCount() > 0);
}
});
}
}
When I try to run my test I got this error :
"Run Android instrumented tests using Gradle" option was ignored because this module type is not supported yet.
My Implementations in build.config file are :
// UNIT TEST
testImplementation 'junit:junit:4.12'
testImplementation 'org.hamcrest:java-hamcrest:2.0.0.0'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.1'
// INSTRUMENTED TEST
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.4.0'

I just ran into this problem, too. I found the solution in the Android Studio Bumblebee 2021.1.1 release notes under the "Android testing" section.
It basically amounts to unchecking the box next to Run Android instrumented tests using Gradle in the testing settings. This worked for me.

the solution is to exclude module: protobuf-lite :
androidTestImplementation('androidx.test.espresso:espresso-contrib:3.4.0') {
exclude module: "protobuf-lite"
}

As mentioned here
Disable the unified Gradle test runner
By default Android Studio Bumblebee uses Gradle to run its instrumentation tests. If you're experiencing issues, you can disable this behavior as follows:
Select File > Settings > Build, Execution, Deployment > Testing (or Android Studio > Preferences > Build, Execution, Deployment > Testing on MacOS.)
Uncheck the box next to Run Android instrumented tests using Gradle and click OK.
So you can just disable the unified Gradle test runner.

For me, the issue was that I was running on Xiaomi Redmi note 7. Once I switched to the emulator. everything worked fine.

Related

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.

Using Androidx test dependencies in Robolectric instrumentation tests

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?

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"

Android Unit Testing Robolectric:3.3.2 - No such manifest file: build\intermediates\manifests\full\debug\src\main\AndroidManifest.xml

when i am running my tests, following error is showing
No such manifest file: build\intermediates\bundles\debug\AndroidManifest.xml
java.lang.ClassCastException: android.app.Application cannot be cast to gyg.android.reviews.ReviewApplication
Following are Gradle dependencies
compile group: 'org.mockito', name: 'mockito-all', version: '2.0.2-beta'
testCompile "org.robolectric:robolectric:3.3.2"
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'junit:junit:4.12'
This is how i am starting my Test Class
#RunWith(RobolectricTestRunner.class)
#Config(constants = BuildConfig.class)
public class ReviewListPresenterTest {
I am using Roboelectric 3.3.2 with Android Studio 2.3.2
Quick help will be highly appreciated !
The problem seems to be with an Android Studio project which contains multiple modules. There are two solutions:
Run the Robolectric tests from the command line:
gradlew :app:testDebugUnitTest
Run the Robolectric tests from Android Studio:
Open the Edit Run Configurations dialog. Under Defaults > Android JUnit, set the Working Directory to the folder which contains the module you are testing. You will probably also need to set this option for each existing run configuration. If you have tests in multiple modules, you will need to set the Working Directory for each run configuration to the appropriate module.

Categories

Resources