How can I test fragments with Robolectric?

Edit #4 & #5: In Robolectric 3.*, they split up the fragment starting functions. For support fragments, you will need to add a dependency to your build.gradle: testCompile “org.robolectric:shadows-supportv4:3.8” Import: org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment; For platform fragments, you don’t need this dependency. Import: import static org.robolectric.util.FragmentTestUtil.startFragment; They both use the same name of startFragment(). import static org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment; @RunWith(RobolectricTestRunner.class) … 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