Robolectric: Resources$NotFoundException: String resource ID with Android Gradle Plugin 3

As mentioned by an engineer from Google team (most possibly Xavier Ducrohet), Robolectric has issues with AAPT2: Robolectric is not compatible with aapt2. Two options here. First option – follow Robolectric guidelines for Android Studio 3.0+ Add the following to your build.gradle: android { testOptions { unitTests { includeAndroidResources = true } } } Annotate … Read more

Providing test data for SharedPreferences for Robolectric

Found out how – seems so obvious now! For those who are interested, you just get the sharedPreferences, and populate it with the required data. SharedPreferences sharedPreferences = ShadowPreferenceManager.getDefaultSharedPreferences(Robolectric.application.getApplicationContext()); sharedPreferences.edit().putString(“testId”, “12345”).commit(); If you have a custom SharedPreferences, you should be able to do this (haven’t really tested properly, but should also work) SharedPreferences sharedPreferences = … Read more

Test running failed: Unable to find instrumentation info for: ComponentInfo{} — error trying to test in IntelliJ with Gradle

I had the same error when I tried adding multiDexEnabled true to build.gradle. I’m adding my experience here, because this is one of the first Google hits when searching with the … Unable to find … ComponentInfo … error message. In my case adding testInstrumentationRunner like here did the trick: … android { … defaultConfig … Read more

registerIdlingResources deprecation replacement doesn’t work

1. Android Test Orchestrator. Android Test Orchestrator resolves your issue with IdlingResources test fails. dependencies { androidTestUtil ‘androidx.test:orchestrator:1.3.0’ } Configure your build.gradle: android { defaultConfig { testInstrumentationRunner “androidx.test.runner.AndroidJUnitRunner” testInstrumentationRunnerArguments clearPackageData: ‘true’ } testOptions { execution ‘ANDROIDX_TEST_ORCHESTRATOR’ } } Then run your tests: ./gradlew connectedCheck 2. Refactor the code The issue IdlingResources cause tests to fail. … Read more

How to use VisibleForTesting for pure JUnit tests

Make the method package-private and the test will be able to see it, if the test is in the corresponding test package (same package name as the production code). @VisibleForTesting Address getAddress() { return mAddress; } Also consider refactoring your code so you don’t need to explicitly test a private method, try testing the behaviour … Read more

Android alpha testing “Item not found”

If the app is “Published”, but when you click in “View in play store” the site (example: https://play.google.com/store/apps/details?id=com.companyname.appname) show the error: “We’re sorry, the requested URL was not found on this server“, try this: In “Google Play Console” (https://play.google.com/apps/publish), select your app goto menu: Release management / App releases In Aplha, click in the button … Read more

androidx.test.InstrumentationRegistry is deprecated

You can use InstrumentationRegistry.getInstrumentation().getTargetContext() in the most cases from androidx.test.platform.app.InstrumentationRegistry. If you need the Application, you can use ApplicationProvider.getApplicationContext<MyAppClass>(). If you haven’t already, I think you can also use the new test dependency: androidTestImplementation ‘androidx.test:core:1.0.0-beta02’.

Espresso – How to check if one of the view is displayed

It’s possible to catch the exceptions raised by Espresso like this: If you want to test if a view is in hierarchy: try { onView(withText(“Button”)).perform(click()); // View is in hierarchy } catch (NoMatchingViewException e) { // View is not in hierarchy } This exception will be thrown if the view is not in the hierarchy. … Read more

How do you test an Android application across multiple Activities?

I feel a bit awkward about answering my own bounty question, but here it is… I’ve searched high and low on this and can’t believe there is no answer published anywhere. I have come very close. I can definitely run tests that span activities now, but my implementation seems to have some timing issues where … Read more

Confused about testCompile and androidTestCompile in Android Gradle

Simply testCompile is the configuration for unit tests (those located in src/test) and androidTestCompile is used for the test api (that located in src/androidTest). Since you are intending to write unit tests, you should use testCompile. Update: The main distinction between the two is the test sourceset runs in a regular Java JVM, whereas the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)