Skip to content

Commit be270b9

Browse files
committed
Fix broken espresso and robolectric tests
1 parent a862b94 commit be270b9

File tree

11 files changed

+113
-38
lines changed

11 files changed

+113
-38
lines changed

app/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ dependencies {
3737
final DAGGER_VERSION = '2.0.1'
3838
final HAMCREST_VERSION = '1.3'
3939
final MOCKITO_VERSION = '1.10.19'
40+
4041
compile fileTree(dir: 'libs', include: ['*.jar'])
42+
4143
compile 'com.android.support:support-v4:22.2.1'
4244
compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION"
4345
compile "com.android.support:cardview-v7:$SUPPORT_LIBRARY_VERSION"
@@ -55,9 +57,11 @@ dependencies {
5557
compile 'com.jakewharton:butterknife:7.0.1'
5658
compile 'com.jakewharton.timber:timber:3.1.0'
5759
compile 'uk.co.ribot:easyadapter:1.5.0@aar'
60+
5861
compile "com.google.dagger:dagger:$DAGGER_VERSION"
5962
apt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
6063
provided 'org.glassfish:javax.annotation:10.0-b28'
64+
6165
testCompile 'junit:junit:4.12'
6266
testCompile "org.hamcrest:hamcrest-core:$HAMCREST_VERSION"
6367
testCompile "org.hamcrest:hamcrest-library:$HAMCREST_VERSION"
@@ -67,6 +71,6 @@ dependencies {
6771
exclude group: 'commons-logging', module: 'commons-logging'
6872
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
6973
}
74+
7075
androidTestApt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
71-
compile 'com.google.dexmaker:dexmaker:1.2'
7276
}

app/src/main/java/com/hitherejoe/androidboilerplate/data/local/DatabaseHelper.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ public List<Character> call(SqlBrite.Query query) {
7474
});
7575
}
7676

77+
public Observable<Void> deleteCharacters() {
78+
return Observable.create(new Observable.OnSubscribe<Void>() {
79+
@Override
80+
public void call(Subscriber<? super Void> subscriber) {
81+
mBriteDb.beginTransaction();
82+
try {
83+
mBriteDb.delete(Db.CharacterTable.TABLE_NAME, null);
84+
mBriteDb.setTransactionSuccessful();
85+
subscriber.onCompleted();
86+
} finally {
87+
mBriteDb.endTransaction();
88+
}
89+
}
90+
});
91+
}
92+
7793
public Observable<Void> clearTables() {
7894
return Observable.create(new Observable.OnSubscribe<Void>() {
7995
@Override

app/src/main/java/com/hitherejoe/androidboilerplate/util/MockModelsUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public static List<String> createMockCollection(int count) {
3232
return items;
3333
}
3434

35-
public static List<Character> createListOfMockCharacters(int[] ids) {
35+
public static List<Character> createListOfMockCharacters(int count) {
3636
List<Character> characters = new ArrayList<>();
37-
for (int id : ids) {
37+
for (int i = 0; i < count; i++) {
3838
characters.add(createMockCharacter());
3939
}
4040
return characters;

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<string name="dialog_general_error_Message">There was an error making the request</string>
4444
<string name="dialog_error_no_connection">Sorry, you need a connection to do that!</string>
4545

46-
46+
<!-- Arrays -->
4747

4848
<integer-array name="characters">
4949
<item>1</item>

app/src/test/java/com/hitherejoe/androidboilerplate/DataManagerTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.junit.Before;
1818
import org.junit.Test;
1919
import org.junit.runner.RunWith;
20-
import org.robolectric.Robolectric;
2120
import org.robolectric.RobolectricTestRunner;
2221
import org.robolectric.RuntimeEnvironment;
2322
import org.robolectric.annotation.Config;
@@ -29,7 +28,6 @@
2928
import rx.schedulers.Schedulers;
3029

3130
import static junit.framework.Assert.assertEquals;
32-
import static org.mockito.Matchers.anyInt;
3331
import static org.mockito.Mockito.mock;
3432
import static org.mockito.Mockito.when;
3533

@@ -45,6 +43,7 @@ public class DataManagerTest {
4543
public void setUp() {
4644
mMockAndroidBoilerplateService = mock(AndroidBoilerplateService.class);
4745
mDatabaseHelper = new DatabaseHelper(RuntimeEnvironment.application);
46+
mDatabaseHelper.clearTables().subscribe();
4847
mDataManager = new DataManager(mMockAndroidBoilerplateService,
4948
mDatabaseHelper,
5049
mock(Bus.class),
@@ -55,7 +54,7 @@ public void setUp() {
5554
@Test
5655
public void shouldSyncCharacters() throws Exception {
5756
int[] ids = new int[]{ 10034, 14050, 10435, 35093 };
58-
List<Character> characters = MockModelsUtil.createListOfMockCharacters(ids);
57+
List<Character> characters = MockModelsUtil.createListOfMockCharacters(4);
5958
for (int i = 0; i < ids.length; i++) {
6059
when(mMockAndroidBoilerplateService.getCharacter(ids[i]))
6160
.thenReturn(Observable.just(characters.get(i)));

app/src/test/java/com/hitherejoe/androidboilerplate/DatabaseHelperTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ public class DatabaseHelperTest {
3131
@Before
3232
public void setUp() {
3333
mDatabaseHelper = new DatabaseHelper(RuntimeEnvironment.application);
34-
mDatabaseHelper.clearTables();
34+
mDatabaseHelper.clearTables().subscribe();
3535
}
3636

3737
@Test
3838
public void shouldSetCharacters() throws Exception {
39-
int[] ids = new int[]{ 10034 };
40-
List<Character> characters = MockModelsUtil.createListOfMockCharacters(ids);
39+
List<Character> characters = MockModelsUtil.createListOfMockCharacters(5);
4140

4241
TestSubscriber<Character> result = new TestSubscriber<>();
4342
mDatabaseHelper.setCharacters(characters).subscribe(result);
@@ -46,7 +45,7 @@ public void shouldSetCharacters() throws Exception {
4645

4746
Cursor cursor = mDatabaseHelper.getBriteDb()
4847
.query("SELECT * FROM " + Db.CharacterTable.TABLE_NAME);
49-
assertEquals(1, cursor.getCount());
48+
assertEquals(5, cursor.getCount());
5049
for (Character character : characters) {
5150
cursor.moveToNext();
5251
assertEquals(character, Db.CharacterTable.parseCursor(cursor));
@@ -55,8 +54,7 @@ public void shouldSetCharacters() throws Exception {
5554

5655
@Test
5756
public void shouldGetCharacters() throws Exception {
58-
int[] ids = new int[]{ 10034 };
59-
List<Character> characters = MockModelsUtil.createListOfMockCharacters(ids);
57+
List<Character> characters = MockModelsUtil.createListOfMockCharacters(5);
6058

6159
mDatabaseHelper.setCharacters(characters).subscribe();
6260

module-test-only/build.gradle

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ dependencies {
2626
compile fileTree(dir: 'libs', include: ['*.jar'])
2727
compile 'com.android.support:appcompat-v7:22.2.1'
2828

29-
compile 'io.reactivex:rxandroid:0.25.0'
30-
compile 'com.jakewharton.timber:timber:3.1.0'
31-
3229
compile "com.google.dagger:dagger:$DAGGER_VERSION"
3330
apt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
3431
provided 'org.glassfish:javax.annotation:10.0-b28'

module-test-only/src/main/java/com/hitherejoe/module_test_only/MainActivityTest.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import com.hitherejoe.androidboilerplate.ui.activity.MainActivity;
1212
import com.hitherejoe.androidboilerplate.util.MockModelsUtil;
1313
import com.hitherejoe.module_test_only.injection.TestComponentRule;
14+
import com.hitherejoe.module_test_only.util.ClearDataRule;
1415

16+
import org.junit.Before;
17+
import org.junit.BeforeClass;
18+
import org.junit.ClassRule;
1519
import org.junit.Rule;
1620
import org.junit.Test;
1721
import org.junit.runner.RunWith;
@@ -41,14 +45,27 @@ public class MainActivityTest {
4145
@Rule
4246
public final TestComponentRule component = new TestComponentRule();
4347

48+
@Rule
49+
public final ClearDataRule clearDataRule = new ClearDataRule(component);
50+
51+
@Before
52+
public void setUp() {
53+
clearDataRule.clearData();
54+
}
55+
4456
@Test
4557
public void testCharactersShowAndAreScrollableInFeed() {
4658
int[] characterIds =
4759
InstrumentationRegistry.getTargetContext().getResources().getIntArray(com.hitherejoe.androidboilerplate.R.array.characters);
48-
List<Character> mockCharacters = MockModelsUtil.createListOfMockCharacters(characterIds);
49-
stubMockPosts(characterIds, mockCharacters);
60+
List<Character> mockCharacters = MockModelsUtil.createListOfMockCharacters(characterIds.length);
61+
stubMockCharacters(characterIds, mockCharacters);
5062
main.launchActivity(null);
51-
checkPostsDisplayOnRecyclerView(mockCharacters);
63+
try {
64+
Thread.sleep(2000);
65+
} catch (InterruptedException e) {
66+
e.printStackTrace();
67+
}
68+
checkCharactersDisplayOnRecyclerView(mockCharacters);
5269
}
5370

5471
@Test
@@ -59,17 +76,17 @@ public void testCharactersNoDescriptionIsShown() {
5976
Character mockCharacter = MockModelsUtil.createMockCharacter();
6077
List<Character> mockCharacters = new ArrayList<>();
6178
mockCharacters.add(mockCharacter);
62-
stubMockPosts(characterIds, mockCharacters);
79+
stubMockCharacters(characterIds, mockCharacters);
6380
main.launchActivity(null);
64-
checkPostsDisplayOnRecyclerView(mockCharacters);
81+
checkCharactersDisplayOnRecyclerView(mockCharacters);
6582
}
6683

6784
@Test
6885
public void testClickOnCardOpensCharacterActivity() {
6986
int[] characterIds =
7087
InstrumentationRegistry.getTargetContext().getResources().getIntArray(com.hitherejoe.androidboilerplate.R.array.characters);
71-
List<Character> mockCharacters = MockModelsUtil.createListOfMockCharacters(characterIds);
72-
stubMockPosts(characterIds, mockCharacters);
88+
List<Character> mockCharacters = MockModelsUtil.createListOfMockCharacters(characterIds.length);
89+
stubMockCharacters(characterIds, mockCharacters);
7390
main.launchActivity(null);
7491
onView(withText(mockCharacters.get(0).name))
7592
.perform(click());
@@ -85,7 +102,7 @@ public void testClickOnView() {
85102
Character mockCharacter = MockModelsUtil.createMockCharacter();
86103
List<Character> mockCharacters = new ArrayList<>();
87104
mockCharacters.add(mockCharacter);
88-
stubMockPosts(characterIds, mockCharacters);
105+
stubMockCharacters(characterIds, mockCharacters);
89106
main.launchActivity(null);
90107
onView(withText("View"))
91108
.perform(click());
@@ -101,36 +118,31 @@ public void testClickOnCollections() {
101118
Character mockCharacter = MockModelsUtil.createMockCharacter();
102119
List<Character> mockCharacters = new ArrayList<>();
103120
mockCharacters.add(mockCharacter);
104-
stubMockPosts(characterIds, mockCharacters);
121+
stubMockCharacters(characterIds, mockCharacters);
105122
main.launchActivity(null);
106123
onView(withText("Collections"))
107124
.perform(click());
108-
onView(withText("Comics"))
125+
onView(withText("Films"))
109126
.check(matches(isDisplayed()));
110127
}
111128

112-
private void checkPostsDisplayOnRecyclerView(List<Character> postsToCheck) {
113-
for (int i = 0; i < postsToCheck.size(); i++) {
129+
private void checkCharactersDisplayOnRecyclerView(List<Character> charactersToCheck) {
130+
for (int i = 0; i < charactersToCheck.size(); i++) {
114131
onView(withId(com.hitherejoe.androidboilerplate.R.id.recycler_characters))
115132
.perform(RecyclerViewActions.scrollToPosition(i));
116-
checkPostDisplays(postsToCheck.get(i));
133+
checkCharacterDisplays(charactersToCheck.get(i));
117134
}
118135
}
119136

120-
private void checkPostDisplays(Character character) {
121-
String films =
122-
InstrumentationRegistry.getTargetContext().getResources().getString(com.hitherejoe.androidboilerplate.R.string.text_films_description, character.films.size());
123-
String noDescription = InstrumentationRegistry.getTargetContext().getResources().getString(com.hitherejoe.androidboilerplate.R.string.text_no_description);
137+
private void checkCharacterDisplays(Character character) {
124138
onView(withText(character.name))
125139
.check(matches(isDisplayed()));
126-
onView(withText(character.films.size() == 0 ? noDescription : films))
127-
.check(matches(isDisplayed()));
128140
}
129141

130-
private void stubMockPosts(int[] ids, List<Character> mockPosts) {
131-
for (int i = 0; i < mockPosts.size(); i++) {
142+
private void stubMockCharacters(int[] ids, List<Character> mockCharacters) {
143+
for (int i = 0; i < mockCharacters.size(); i++) {
132144
when(component.getMockWatchTowerService().getCharacter(ids[i]))
133-
.thenReturn(Observable.just(MockModelsUtil.createMockCharacter()));
145+
.thenReturn(Observable.just(mockCharacters.get(i)));
134146
}
135147
}
136148
}

module-test-only/src/main/java/com/hitherejoe/module_test_only/injection/TestComponentRule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.support.test.InstrumentationRegistry;
55

66
import com.hitherejoe.androidboilerplate.AndroidBoilerplateApplication;
7+
import com.hitherejoe.androidboilerplate.data.local.DatabaseHelper;
78
import com.hitherejoe.androidboilerplate.data.local.PreferencesHelper;
89
import com.hitherejoe.androidboilerplate.data.remote.AndroidBoilerplateService;
910
import com.hitherejoe.module_test_only.injection.component.DaggerTestComponent;
@@ -31,6 +32,10 @@ public TestDataManager getDataManager() {
3132
return (TestDataManager) mTestComponent.dataManager();
3233
}
3334

35+
public DatabaseHelper getDatabaseHelper() {
36+
return getDataManager().getDatabaseHelper();
37+
}
38+
3439
public AndroidBoilerplateService getMockWatchTowerService() {
3540
return getDataManager().getAndroidBoilerplateService();
3641
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.hitherejoe.module_test_only.util;
2+
3+
import com.hitherejoe.module_test_only.injection.TestComponentRule;
4+
5+
import org.junit.rules.TestRule;
6+
import org.junit.runner.Description;
7+
import org.junit.runners.model.Statement;
8+
9+
/**
10+
* Test rule that given a TestComponentRule clears the local data such as databases or
11+
* shared preferences
12+
*/
13+
public class ClearDataRule implements TestRule {
14+
15+
private TestComponentRule mTestComponentRule;
16+
17+
public ClearDataRule(TestComponentRule testComponentRule) {
18+
mTestComponentRule = testComponentRule;
19+
}
20+
21+
public void clearData() {
22+
mTestComponentRule.getDatabaseHelper().clearTables().subscribe();
23+
}
24+
25+
@Override
26+
public Statement apply(final Statement base, Description description) {
27+
return new Statement() {
28+
@Override
29+
public void evaluate() throws Throwable {
30+
try {
31+
clearData();
32+
base.evaluate();
33+
} finally {
34+
mTestComponentRule = null;
35+
}
36+
}
37+
};
38+
}
39+
}

0 commit comments

Comments
 (0)