-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Functional test with AndroidTestRule #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,13 @@ dependencies { | |
androidTestCompile presentationTestDependencies.mockito | ||
androidTestCompile presentationTestDependencies.dexmaker | ||
androidTestCompile presentationTestDependencies.dexmakerMockito | ||
androidTestCompile presentationTestDependencies.espresso | ||
androidTestCompile presentationTestDependencies.testingSupportLib | ||
androidTestCompile (presentationTestDependencies.espresso) { | ||
exclude group: 'com.android.support', module: 'support-annotations' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to exclude annotations here? Is this a transitive dependency? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a conflict with dependencies, because of different versions of android support annotation. If I remove this line a I get this error :
|
||
} | ||
androidTestCompile (presentationTestDependencies.testRules) { | ||
exclude group: 'com.android.support', module: 'support-annotations' | ||
} | ||
androidTestCompile (presentationTestDependencies.testRunner) { | ||
exclude group: 'com.android.support', module: 'support-annotations' | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,9 +17,15 @@ | |
|
||
import android.app.Fragment; | ||
import android.content.Intent; | ||
import android.test.ActivityInstrumentationTestCase2; | ||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.rule.ActivityTestRule; | ||
import android.support.test.runner.AndroidJUnit4; | ||
import com.fernandocejas.android10.sample.presentation.R; | ||
import com.fernandocejas.android10.sample.presentation.view.activity.UserDetailsActivity; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static android.support.test.espresso.Espresso.onView; | ||
import static android.support.test.espresso.assertion.ViewAssertions.matches; | ||
|
@@ -31,38 +37,39 @@ | |
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.not; | ||
|
||
public class UserDetailsActivityTest extends ActivityInstrumentationTestCase2<UserDetailsActivity> { | ||
@RunWith(AndroidJUnit4.class) | ||
public class UserDetailsActivityTest { | ||
|
||
private static final int FAKE_USER_ID = 10; | ||
|
||
private UserDetailsActivity userDetailsActivity; | ||
|
||
public UserDetailsActivityTest() { | ||
super(UserDetailsActivity.class); | ||
} | ||
@Rule public ActivityTestRule<UserDetailsActivity> activityRule = new ActivityTestRule<>( | ||
UserDetailsActivity.class, | ||
true, // initialTouchMode | ||
false); // launchActivity. False to set up mocks before activity launch | ||
|
||
@Override protected void setUp() throws Exception { | ||
super.setUp(); | ||
this.setActivityIntent(createTargetIntent()); | ||
this.userDetailsActivity = getActivity(); | ||
} | ||
private UserDetailsActivity userDetailsActivity; | ||
|
||
@Override protected void tearDown() throws Exception { | ||
super.tearDown(); | ||
@Before public void setUp() { | ||
activityRule.launchActivity(createTargetIntent()); | ||
userDetailsActivity = activityRule.getActivity(); | ||
} | ||
|
||
@Test | ||
public void testContainsUserDetailsFragment() { | ||
Fragment userDetailsFragment = | ||
userDetailsActivity.getFragmentManager().findFragmentById(R.id.fl_fragment); | ||
assertThat(userDetailsFragment, is(notNullValue())); | ||
} | ||
|
||
@Test | ||
public void testContainsProperTitle() { | ||
|
||
String actualTitle = this.userDetailsActivity.getTitle().toString().trim(); | ||
|
||
assertThat(actualTitle, is("User Details")); | ||
} | ||
|
||
@Test | ||
public void testLoadUserHappyCaseViews() { | ||
onView(withId(R.id.rl_retry)).check(matches(not(isDisplayed()))); | ||
onView(withId(R.id.rl_progress)).check(matches(not(isDisplayed()))); | ||
|
@@ -72,6 +79,7 @@ public void testLoadUserHappyCaseViews() { | |
onView(withId(R.id.tv_description)).check(matches(isDisplayed())); | ||
} | ||
|
||
@Test | ||
public void testLoadUserHappyCaseData() { | ||
onView(withId(R.id.tv_fullname)).check(matches(withText("John Sanchez"))); | ||
onView(withId(R.id.tv_email)).check(matches(withText("[email protected]"))); | ||
|
@@ -80,7 +88,7 @@ public void testLoadUserHappyCaseData() { | |
|
||
private Intent createTargetIntent() { | ||
Intent intentLaunchActivity = | ||
UserDetailsActivity.getCallingIntent(getInstrumentation().getTargetContext(), FAKE_USER_ID); | ||
UserDetailsActivity.getCallingIntent(InstrumentationRegistry.getTargetContext(), FAKE_USER_ID); | ||
|
||
return intentLaunchActivity; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MehdiChouag this must be
com.android.support.test:runner
not rules.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spirosoik Just for the line 51 or for both 50 and 51 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MehdiChouag for 51. Now you have twice the same thing but you forgot the runner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, thanks I'll modify it.