Skip to content

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
language: android
jdk: oraclejdk8

before_script:
- echo y | android update sdk --no-ui --all --filter build-tools-23.0.1
- echo y | android update sdk --no-ui --all --filter android-23
- echo y | android update sdk --no-ui --all --filter extra-android-support
- echo y | android update sdk --no-ui --all --filter extra-android-m2repository

android:
components:
- build-tools-23.0.1
Expand Down
13 changes: 7 additions & 6 deletions buildsystem/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ ext {
daggerVersion = '2.0.2'
butterKnifeVersion = '7.0.1'
recyclerViewVersion = '21.0.3'
rxJavaVersion = '1.0.14'
rxJavaVersion = '1.0.16'
rxAndroidVersion = '1.0.1'
javaxAnnotationVersion = '1.0'
gsonVersion = '2.3'
okHttpVersion = '2.5.0'
gsonVersion = '2.4'
okHttpVersion = '2.6.0'
androidAnnotationsVersion = '21.0.3'

//Testing
Expand All @@ -28,8 +28,8 @@ ext {
assertJVersion = '1.7.1'
mockitoVersion = '1.9.5'
dexmakerVersion = '1.0'
espressoVersion = '2.0'
testingSupportLibVersion = '0.1'
espressoVersion = '2.2.1'
testingSupportLibVersion = '0.4.1'


presentationDependencies = [
Expand All @@ -47,7 +47,8 @@ ext {
dexmaker: "com.google.dexmaker:dexmaker:${dexmakerVersion}",
dexmakerMockito: "com.google.dexmaker:dexmaker-mockito:${dexmakerVersion}",
espresso: "com.android.support.test.espresso:espresso-core:${espressoVersion}",
testingSupportLib: "com.android.support.test:testing-support-lib:${testingSupportLibVersion}",
testRules: "com.android.support.test:rules:${testingSupportLibVersion}",
testRunner: "com.android.support.test:rules:${testingSupportLibVersion}"

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.

Copy link
Author

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 ?

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.

Copy link
Author

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.

]

domainDependencies = [
Expand Down
11 changes: 9 additions & 2 deletions presentation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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 :

Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (21.0.3) and test app (23.0.1) differ.

}
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
Expand Up @@ -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;
Expand All @@ -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())));
Expand All @@ -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]")));
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,43 @@

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.UserListActivity;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

public class UserListActivityTest extends ActivityInstrumentationTestCase2<UserListActivity> {
@RunWith(AndroidJUnit4.class)
public class UserListActivityTest {

private UserListActivity userListActivity;

public UserListActivityTest() {
super(UserListActivity.class);
}

@Override protected void setUp() throws Exception {
super.setUp();
this.setActivityIntent(createTargetIntent());
userListActivity = getActivity();
}
@Rule public ActivityTestRule<UserListActivity> activityRule = new ActivityTestRule<>(
UserListActivity.class,
true, // initialTouchMode
false); // launchActivity. False to set up mocks before activity launch

@Override protected void tearDown() throws Exception {
super.tearDown();
@Before public void setUp() {
activityRule.launchActivity(createTargetIntent());
userListActivity = activityRule.getActivity();
}

@Test
public void testContainsUserListFragment() {
Fragment userListFragment =
userListActivity.getFragmentManager().findFragmentById(R.id.fragmentUserList);
assertThat(userListFragment, is(notNullValue()));
}

@Test
public void testContainsProperTitle() {
String actualTitle = this.userListActivity.getTitle().toString().trim();

Expand All @@ -57,7 +62,7 @@ public void testContainsProperTitle() {

private Intent createTargetIntent() {
Intent intentLaunchActivity =
UserListActivity.getCallingIntent(getInstrumentation().getTargetContext());
UserListActivity.getCallingIntent(InstrumentationRegistry.getTargetContext());

return intentLaunchActivity;
}
Expand Down