Last active
February 4, 2018 18:02
-
-
Save mohsenoid/0ef350f2f24c48b5f67cf409d1bee25a to your computer and use it in GitHub Desktop.
Sample ActivityTest using espresso https://hackernoon.com/yet-another-mvp-article-part-5-writing-test-using-a-mixture-of-dagger-and-espresso-15c638182706
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mirhoseini.marvel.activity; | |
/*...*/ | |
@RunWith(AndroidJUnit4.class) | |
public class MainActivityTest { | |
private static final String TEST_CHARACTER_NAME = "Test Name"; | |
private static final String TEST_CHARACTER_DESCRIPTION = "Test Description"; | |
private static final String TEST_CHARACTER_THUMBNAIL_PATH = "Test Thumbnail"; | |
private static final String TEST_CHARACTER_THUMBNAIL_EXTENSION = "Test Extension"; | |
@Rule | |
public ActivityTestRule<MainActivity> mainActivity = new ActivityTestRule<>( | |
MainActivity.class, | |
true, | |
// false: do not launch the activity immediately | |
false); | |
@Inject | |
MarvelApi api; | |
CharactersResponse expectedResult; | |
@Before | |
public void setUp() { | |
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); | |
MarvelTestApplication app = (MarvelTestApplication) instrumentation.getTargetContext().getApplicationContext(); | |
ApplicationTestComponent component = (ApplicationTestComponent) MarvelApplication.getComponent(); | |
// inject dagger | |
component.inject(this); | |
// Set up the stub we want to return in the mock | |
Results character = new Results(); | |
character.setName(TEST_CHARACTER_NAME); | |
character.setDescription(TEST_CHARACTER_DESCRIPTION); | |
character.setThumbnail(new Thumbnail(TEST_CHARACTER_THUMBNAIL_PATH, TEST_CHARACTER_THUMBNAIL_EXTENSION)); | |
Results[] characters = new Results[1]; | |
characters[0] = character; | |
Data charactersData = new Data(); | |
charactersData.setCount(1); | |
charactersData.setResults(characters); | |
// put the test character in a test api result | |
expectedResult = new CharactersResponse(); | |
expectedResult.setData(charactersData); | |
expectedResult.setCode(Constants.CODE_OK); | |
// Set up the mock | |
when(api.getCharacters(eq(TEST_CHARACTER_NAME), any(String.class), any(String.class), any(Long.class))) | |
.thenReturn(Observable.just(expectedResult)); | |
} | |
@Test | |
public void shouldBeAbleToSearchForTestCharacter() { | |
// Launch the activity | |
mainActivity.launchActivity(new Intent()); | |
// search for Test Character | |
ViewInteraction appCompatAutoCompleteTextView = onView( | |
allOf(withId(R.id.character), isDisplayed())); | |
appCompatAutoCompleteTextView.perform(replaceText(TEST_CHARACTER_NAME)); | |
ViewInteraction appCompatButton = onView( | |
allOf(withId(R.id.show), isDisplayed())); | |
appCompatButton.perform(click()); | |
// Check view is loaded as we expect it to be | |
onView(withText(TEST_CHARACTER_NAME)).check(matches(withText(TEST_CHARACTER_NAME))); | |
onView(withId(R.id.description)).check(matches(withText(TEST_CHARACTER_DESCRIPTION))); | |
} | |
@Test | |
public void shouldBeAbleToCacheTestCharacter() { | |
// Launch the activity | |
mainActivity.launchActivity(new Intent()); | |
// search for Test Character | |
ViewInteraction appCompatAutoCompleteTextView = onView( | |
allOf(withId(R.id.character), isDisplayed())); | |
appCompatAutoCompleteTextView.perform(replaceText(TEST_CHARACTER_NAME)); | |
ViewInteraction appCompatButton = onView( | |
allOf(withId(R.id.show), isDisplayed())); | |
appCompatButton.perform(click()); | |
pressBack(); | |
// Check view is loaded as we expect it to be | |
ViewInteraction cachedName = onView( | |
allOf(withId(R.id.name), withText(TEST_CHARACTER_NAME), | |
childAtPosition( | |
childAtPosition( | |
IsInstanceOf.instanceOf(android.widget.FrameLayout.class), | |
0), | |
1), | |
isDisplayed())); | |
cachedName.check(matches(withText(TEST_CHARACTER_NAME))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment