Skip to content

Commit 317dd33

Browse files
committed
Migrate data unit tests to robolectric 3. Use android unit test support feature.
1 parent 9d49503 commit 317dd33

14 files changed

+699
-6
lines changed

build.gradle

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ task wrapper(type: Wrapper) {
2626
gradleVersion '2.4'
2727
}
2828

29-
task runAcceptanceTests(dependsOn: [':presentation:connectedAndroidTest']) {
30-
description 'Run application acceptance tests.'
29+
task runDomainUnitTests(dependsOn: [':domain:test']) {
30+
description 'Run unit tests for both domain and data layers.'
31+
}
32+
33+
task runDataUnitTests(dependsOn: [':data:cleanTestDebug', ':data:testDebug']) {
34+
description 'Run unit tests for both domain and data layers.'
3135
}
3236

33-
task runUnitTests(dependsOn: [':domain:test', ':data-test:test']) {
37+
task runUnitTests(dependsOn: ['runDomainUnitTests', 'runDataUnitTests']) {
3438
description 'Run unit tests for both domain and data layers.'
3539
}
3640

41+
task runAcceptanceTests(dependsOn: [':presentation:connectedAndroidTest']) {
42+
description 'Run application acceptance tests.'
43+
}
44+

buildsystem/dependencies.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ ext {
2323
androidAnnotationsVersion = '20.0.0'
2424

2525
//Testing
26-
robolectricVersion = '2.4'
27-
jUnitVersion = '4.11'
26+
robolectricVersion = '3.0-rc3'
27+
jUnitVersion = '4.12'
28+
assertJVersion = '1.7.1'
2829
mockitoVersion = '1.9.5'
2930
dexmakerVersion = '1.0'
3031
espressoVersion = '2.0'
@@ -74,6 +75,7 @@ ext {
7475

7576
dataTestDependencies = [
7677
junit: "junit:junit:${jUnitVersion}",
78+
assertj: "org.assertj:assertj-core:${assertJVersion}",
7779
mockito: "org.mockito:mockito-core:${mockitoVersion}",
7880
robolectric: "org.robolectric:robolectric:${robolectricVersion}",
7981
]

data/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ android {
5151

5252
dependencies {
5353
def dataDependencies = rootProject.ext.dataDependencies
54+
def testDependencies = rootProject.ext.dataTestDependencies
5455

5556
compile project(':domain')
56-
5757
apt dataDependencies.daggerCompiler
5858
provided dataDependencies.javaxAnnotation
5959
compile dataDependencies.dagger
@@ -62,4 +62,9 @@ dependencies {
6262
compile dataDependencies.rxJava
6363
compile dataDependencies.rxAndroid
6464
compile dataDependencies.androidAnnotations
65+
66+
testCompile testDependencies.junit
67+
testCompile testDependencies.assertj
68+
testCompile testDependencies.mockito
69+
testCompile testDependencies.robolectric
6570
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (C) 2015 android10.org Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.fernandocejas.android10.sample.data;
17+
18+
import android.app.Application;
19+
20+
public class ApplicationStub extends Application {}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (C) 2015 Fernando Cejas Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.fernandocejas.android10.sample.data;
17+
18+
import org.junit.runner.RunWith;
19+
import org.robolectric.RobolectricGradleTestRunner;
20+
import org.robolectric.annotation.Config;
21+
22+
/**
23+
* Base class for Robolectric data layer tests.
24+
* Inherit from this class to create a test.
25+
*/
26+
@RunWith(RobolectricGradleTestRunner.class)
27+
@Config(constants = BuildConfig.class, application = ApplicationStub.class)
28+
public abstract class ApplicationTestCase {}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright (C) 2015 Fernando Cejas Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.fernandocejas.android10.sample.data.cache;
17+
18+
import com.fernandocejas.android10.sample.data.ApplicationTestCase;
19+
import java.io.File;
20+
import org.junit.After;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.robolectric.RuntimeEnvironment;
24+
25+
import static org.hamcrest.CoreMatchers.equalTo;
26+
import static org.hamcrest.CoreMatchers.is;
27+
import static org.hamcrest.MatcherAssert.assertThat;
28+
29+
public class FileManagerTest extends ApplicationTestCase {
30+
31+
private FileManager fileManager;
32+
private File cacheDir;
33+
34+
@Before
35+
public void setUp() {
36+
fileManager = new FileManager();
37+
cacheDir = RuntimeEnvironment.application.getCacheDir();
38+
}
39+
40+
@After
41+
public void tearDown() {
42+
if (cacheDir != null) {
43+
fileManager.clearDirectory(cacheDir);
44+
}
45+
}
46+
47+
@Test
48+
public void testWriteToFile() {
49+
File fileToWrite = createDummyFile();
50+
String fileContent = "content";
51+
52+
fileManager.writeToFile(fileToWrite, fileContent);
53+
54+
assertThat(fileToWrite.exists(), is(true));
55+
}
56+
57+
@Test
58+
public void testFileContent() {
59+
File fileToWrite = createDummyFile();
60+
String fileContent = "content\n";
61+
62+
fileManager.writeToFile(fileToWrite, fileContent);
63+
String expectedContent = fileManager.readFileContent(fileToWrite);
64+
65+
assertThat(expectedContent, is(equalTo(fileContent)));
66+
}
67+
68+
private File createDummyFile() {
69+
String dummyFilePath = cacheDir.getPath() + File.separator + "dumyFile";
70+
File dummyFile = new File(dummyFilePath);
71+
72+
return dummyFile;
73+
}
74+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright (C) 2015 Fernando Cejas Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.fernandocejas.android10.sample.data.cache.serializer;
17+
18+
import com.fernandocejas.android10.sample.data.ApplicationTestCase;
19+
import com.fernandocejas.android10.sample.data.entity.UserEntity;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
23+
import static org.hamcrest.CoreMatchers.equalTo;
24+
import static org.hamcrest.CoreMatchers.is;
25+
import static org.junit.Assert.assertThat;
26+
27+
public class JsonSerializerTest extends ApplicationTestCase {
28+
29+
private static final String JSON_RESPONSE = "{\n"
30+
+ " \"id\": 1,\n"
31+
+ " \"cover_url\": \"http://www.android10.org/myapi/cover_1.jpg\",\n"
32+
+ " \"full_name\": \"Simon Hill\",\n"
33+
+ " \"description\": \"Curabitur gravida nisi at nibh. In hac habitasse platea dictumst. Aliquam augue quam, sollicitudin vitae, consectetuer eget, rutrum at, lorem.\\n\\nInteger tincidunt ante vel ipsum. Praesent blandit lacinia erat. Vestibulum sed magna at nunc commodo placerat.\\n\\nPraesent blandit. Nam nulla. Integer pede justo, lacinia eget, tincidunt eget, tempus vel, pede.\",\n"
34+
+ " \"followers\": 7484,\n"
35+
+ " \"email\": \"[email protected]\"\n"
36+
+ "}";
37+
38+
private JsonSerializer jsonSerializer;
39+
40+
@Before
41+
public void setUp() {
42+
jsonSerializer = new JsonSerializer();
43+
}
44+
45+
@Test
46+
public void testSerializeHappyCase() {
47+
UserEntity userEntityOne = jsonSerializer.deserialize(JSON_RESPONSE);
48+
String jsonString = jsonSerializer.serialize(userEntityOne);
49+
UserEntity userEntityTwo = jsonSerializer.deserialize(jsonString);
50+
51+
assertThat(userEntityOne.getUserId(), is(userEntityTwo.getUserId()));
52+
assertThat(userEntityOne.getFullname(), is(equalTo(userEntityTwo.getFullname())));
53+
assertThat(userEntityOne.getFollowers(), is(userEntityTwo.getFollowers()));
54+
}
55+
56+
@Test
57+
public void testDesearializeHappyCase() {
58+
UserEntity userEntity = jsonSerializer.deserialize(JSON_RESPONSE);
59+
60+
assertThat(userEntity.getUserId(), is(1));
61+
assertThat(userEntity.getFullname(), is("Simon Hill"));
62+
assertThat(userEntity.getFollowers(), is(7484));
63+
}
64+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright (C) 2015 Fernando Cejas Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.fernandocejas.android10.sample.data.entity.mapper;
17+
18+
import com.fernandocejas.android10.sample.data.ApplicationTestCase;
19+
import com.fernandocejas.android10.sample.data.entity.UserEntity;
20+
import com.fernandocejas.android10.sample.domain.User;
21+
import java.util.ArrayList;
22+
import java.util.Collection;
23+
import java.util.List;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
import static org.hamcrest.CoreMatchers.instanceOf;
28+
import static org.hamcrest.CoreMatchers.is;
29+
import static org.hamcrest.MatcherAssert.assertThat;
30+
import static org.mockito.Mockito.mock;
31+
32+
public class UserEntityDataMapperTest extends ApplicationTestCase {
33+
34+
private static final int FAKE_USER_ID = 123;
35+
private static final String FAKE_FULLNAME = "Tony Stark";
36+
37+
private UserEntityDataMapper userEntityDataMapper;
38+
39+
@Before
40+
public void setUp() throws Exception {
41+
userEntityDataMapper = new UserEntityDataMapper();
42+
}
43+
44+
@Test
45+
public void testTransformUserEntity() {
46+
UserEntity userEntity = createFakeUserEntity();
47+
User user = userEntityDataMapper.transform(userEntity);
48+
49+
assertThat(user, is(instanceOf(User.class)));
50+
assertThat(user.getUserId(), is(FAKE_USER_ID));
51+
assertThat(user.getFullName(), is(FAKE_FULLNAME));
52+
}
53+
54+
@Test
55+
public void testTransformUserEntityCollection() {
56+
UserEntity mockUserEntityOne = mock(UserEntity.class);
57+
UserEntity mockUserEntityTwo = mock(UserEntity.class);
58+
59+
List<UserEntity> userEntityList = new ArrayList<UserEntity>(5);
60+
userEntityList.add(mockUserEntityOne);
61+
userEntityList.add(mockUserEntityTwo);
62+
63+
Collection<User> userCollection = userEntityDataMapper.transform(userEntityList);
64+
65+
assertThat(userCollection.toArray()[0], is(instanceOf(User.class)));
66+
assertThat(userCollection.toArray()[1], is(instanceOf(User.class)));
67+
assertThat(userCollection.size(), is(2));
68+
}
69+
70+
private UserEntity createFakeUserEntity() {
71+
UserEntity userEntity = new UserEntity();
72+
userEntity.setUserId(FAKE_USER_ID);
73+
userEntity.setFullname(FAKE_FULLNAME);
74+
75+
return userEntity;
76+
}
77+
}

0 commit comments

Comments
 (0)