Skip to content

Instantly share code, notes, and snippets.

@abatilo
Created February 27, 2017 00:04
Show Gist options
  • Save abatilo/4b6111d9e12058b3573f87df8cb44d6d to your computer and use it in GitHub Desktop.
Save abatilo/4b6111d9e12058b3573f87df8cb44d6d to your computer and use it in GitHub Desktop.
@RunWith(AndroidJUnit4.class) public class FragmentTest {
// Setup an ActivityTestRule as such that the Activity is not launched until we say so
@Rule public ActivityTestRule<EmptyActivity> rule =
new ActivityTestRule<>(EmptyActivity.class, true, false);
private Fragment fragment;
private EmptyActivity activity;
@Before public void setup() {
// Setup shared mock information or do your dependency injection
fragment = new YourFragment();
}
@Test public void fragmentTest() {
// Setup your test specific mocks here
activity = rule.launchActivity(new Intent());
activity.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment, fragment)
.commit();
// Write your assertions here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment