package com.mani.fuellog; @RunWith(AndroidJUnit4.class) public class HomeFragmentTest { @Mock private FuelLogViewModel fuelLogViewModel; private MutableLiveData doubleMutableLiveData = new MutableLiveData<>(); @Before public void setUp() { MockitoAnnotations.initMocks(this); when(fuelLogViewModel.getAverageFuelConsumption()).thenReturn(doubleMutableLiveData); FragmentScenario.launchInContainer(HomeFragment.class,null,new FragmentFactory(){ @NonNull @Override public Fragment instantiate(@NonNull ClassLoader classLoader, @NonNull String className) { return HomeFragment.newInstance(ViewModelUtil.createFor(fuelLogViewModel)); } }).moveToState(Lifecycle.State.RESUMED); } @Test public void shouldDisplay_averageConsumptionValue_postedValidDoubleOnMutableLiveData() { doubleMutableLiveData.postValue(new FuelStat(4.5d,4.5d,0,0,0)); onView(withId(R.id.average_consumption_value)).check(matches(withText("4.5"))); onView(withId(R.id.driving_cost_value)).check(matches(withText("4.5"))); onView(withId(R.id.total_distance_value)).check(matches(withText("0"))); onView(withId(R.id.total_amount_value)).check(matches(withText("0"))); onView(withId(R.id.total_consumption_value)).check(matches(withText("0"))); } @Test public void shouldDisplay_averageConsumptionValue_postedNullOnMutableLiveData() { doubleMutableLiveData.postValue(null); onView(withId(R.id.average_consumption_value)).check(matches(withText("--"))); onView(withId(R.id.driving_cost_value)).check(matches(withText("--"))); onView(withId(R.id.total_distance_value)).check(matches(withText("--"))); onView(withId(R.id.total_amount_value)).check(matches(withText("--"))); onView(withId(R.id.total_consumption_value)).check(matches(withText("--"))); } }