Skip to content

Instantly share code, notes, and snippets.

@Manikkumar1988
Created May 9, 2019 00:07
Show Gist options
  • Save Manikkumar1988/005a8ef4979718ad5c68dfe21ea101b4 to your computer and use it in GitHub Desktop.
Save Manikkumar1988/005a8ef4979718ad5c68dfe21ea101b4 to your computer and use it in GitHub Desktop.
package com.mani.fuellog;
@RunWith(AndroidJUnit4.class)
public class HomeFragmentTest {
@Mock
private FuelLogViewModel fuelLogViewModel;
private MutableLiveData<FuelStat> 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("--")));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment