Skip to content

Commit bd52f20

Browse files
authored
Merge pull request #2 from pzanzane/viewmodel_test
Added Roboelectric and AndroidX Test suit
2 parents 3425ad6 + 6154279 commit bd52f20

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

app/build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ apply plugin: "androidx.navigation.safeargs.kotlin"
66
android {
77
compileSdkVersion rootProject.compileSdkVersion
88

9+
/**
10+
* Allows to access Android Resources in your unit tests, including AndroidManifest file.
11+
* This will resolve "no such as manifest file: " warning.
12+
*/
13+
testOptions.unitTests {
14+
includeAndroidResources = true
15+
}
16+
917
defaultConfig {
1018
applicationId "com.example.android.architecture.blueprints.reactive"
1119
minSdkVersion rootProject.minSdkVersion
@@ -54,6 +62,11 @@ dependencies {
5462
testImplementation "junit:junit:$junitVersion"
5563
testImplementation "com.google.truth:truth:1.1.2"
5664

65+
// AndroidX Test - JVM testing
66+
testImplementation "androidx.test.ext:junit-ktx:$androidXTestExtKotlinRunnerVersion"
67+
testImplementation "androidx.test:core-ktx:$androidXTestCoreVersion"
68+
testImplementation "org.robolectric:robolectric:$robolectricVersion"
69+
5770
// AndroidX Test - Instrumented testing
5871
androidTestImplementation "androidx.test.ext:junit:$androidXTestExtKotlinRunnerVersion"
5972
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"

app/src/test/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsUtilsTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package com.example.android.architecture.blueprints.todoapp.statistics
33
import com.example.android.architecture.blueprints.todoapp.data.Task
44
import com.google.common.truth.Truth
55
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
import org.junit.runners.JUnit4
68

79

10+
@RunWith(JUnit4::class)
811
internal class StatisticsUtilsTest {
912

1013

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.android.architecture.blueprints.todoapp.tasks
2+
3+
import androidx.test.core.app.ApplicationProvider
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
8+
9+
@RunWith(AndroidJUnit4::class)
10+
internal class TasksViewModelTest {
11+
12+
@Test
13+
fun addNewTask_setNewTask() {
14+
15+
// Assign a ViewModel
16+
val tasksViewModel = TasksViewModel(ApplicationProvider.getApplicationContext())
17+
18+
// Assert adding a new Task
19+
tasksViewModel.addNewTask()
20+
21+
// Act new Task is added
22+
// TODO: Test live data.
23+
}
24+
}

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ buildscript {
55
google()
66
mavenCentral()
77
}
8+
89
dependencies {
910
classpath 'com.android.tools.build:gradle:7.0.3'
1011
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
@@ -46,6 +47,7 @@ ext {
4647
recyclerViewVersion = '1.2.1'
4748
roomVersion = '2.3.0'
4849
rulesVersion = '1.0.1'
50+
robolectricVersion = '4.10.3'
4951
swipeRefreshLayoutVersion = '1.1.0'
5052
timberVersion = '4.7.1'
5153
}

0 commit comments

Comments
 (0)