A couple of days ago, androidx.test libraries were published, finally.
https://developer.android.com/training/testing/release-notes
Then, Robolectric4 was also published.
https://github.com/robolectric/robolectric/releases/tag/robolectric-4.0
The benefit of Robolectric was making android test fast with some mocking Android APIs. Meanwhile, the mocking leaded wrong test results, sometimes. I faced many times such case.
According to the release note, Robolectric has also a bunch of improvements to make it Android API friendly. We probably can trust the behaviour than 3.8 and below, I believe. But don’t forget to depends the mocking. You can see below lines in http://robolectric.org/migrating/#migrating-to-40
Robolectric 4.0 includes initial support for androidx.test APIs. We strongly recommend adding androidx.test:core:1.0.0 as a test dependency and using those APIs whenever possible rather than using Robolectric-specific APIs.
Compare context
Below two example is interesting, I think. Test code is almost same. The only difference is the result of InstrumentationRegistry.getInstrumentation().context.
Android API
Run with connectedAndroidTest. Test code is in androidTest directory.
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
val classNam: String = this.javaClass.name
@Test
fun targetContext() {
val targetContext = InstrumentationRegistry.getInstrumentation().targetContext
assertThat(targetContext.packageName).isEqualTo("com.example.kazuaki.espressoenv")
}
@Test
fun context() {
val context = InstrumentationRegistry.getInstrumentation().context
assertThat(context.packageName).isEqualTo("com.example.kazuaki.espressoenv.test")
}
}
Robolectric
Run with test. Test code is in test directory.
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
val classNam: String = this.javaClass.name
@Test
fun targetContext() {
val targetContext = InstrumentationRegistry.getInstrumentation().targetContext
assertThat(targetContext.packageName).isEqualTo("com.example.kazuaki.espressoenv")
}
@Test
fun context() {
val context = InstrumentationRegistry.getInstrumentation().context
assertThat(context.packageName).isEqualTo("com.example.kazuaki.espressoenv")
}
}
useLibrary
If you set both target version and minimum SDK version as Android 9 or higher, you should follow https://developer.android.com/training/testing/set-up-project#junit-based-libs
Make sure JUnit-based libraries section to understand which package has which libraries. I think they are a bit complex for all of developers.
Except for the above, you can see many changes in the androidx and robolectric.