Final Results
Our updated app/build.gradle
should resemble:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'androidx.navigation.safeargs.kotlin'
id 'kotlin-kapt'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.commonsware.todo"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
viewBinding true
}
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
packagingOptions {
exclude 'META-INF/AL2.0'
exclude 'META-INF/LGPL2.1'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation "androidx.recyclerview:recyclerview:1.2.1"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
implementation 'com.google.android.material:material:1.4.0'
implementation "io.insert-koin:koin-android:$koin_version"
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
kapt "androidx.room:room-compiler:$room_version"
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
testImplementation 'junit:junit:4.13.2'
testImplementation "org.mockito:mockito-inline:3.12.1"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.1'
}
And our new ToDoRepositoryTest
should contain:
package com.commonsware.todo.repo
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runBlockingTest
import org.hamcrest.Matchers.empty
import org.hamcrest.Matchers.equalTo
import org.hamcrest.collection.IsIterableContainingInOrder.contains
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class ToDoRepositoryTest {
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()
private val context = InstrumentationRegistry.getInstrumentation().targetContext
private val db = ToDoDatabase.newTestInstance(context)
@Test
fun canAddItems() = runBlockingTest {
val underTest = ToDoRepository(db.todoStore(), this)
val results = mutableListOf<List<ToDoModel>>()
val itemsJob = launch {
underTest.items().collect { results.add(it) }
}
assertThat(results.size, equalTo(1))
assertThat(results[0], empty())
val testModel = ToDoModel("test model")
underTest.save(testModel)
assertThat(results.size, equalTo(2))
assertThat(results[1], contains(testModel))
assertThat(underTest.find(testModel.id).first(), equalTo(testModel))
itemsJob.cancel()
}
@Test
fun canModifyItems() = runBlockingTest {
val underTest = ToDoRepository(db.todoStore(), this)
val testModel = ToDoModel("test model")
val replacement = testModel.copy(notes = "This is the replacement")
val results = mutableListOf<List<ToDoModel>>()
val itemsJob = launch {
underTest.items().collect { results.add(it) }
}
assertThat(results[0], empty())
underTest.save(testModel)
assertThat(results[1], contains(testModel))
underTest.save(replacement)
assertThat(results[2], contains(replacement))
itemsJob.cancel()
}
@Test
fun canRemoveItems() = runBlockingTest {
val underTest = ToDoRepository(db.todoStore(), this)
val testModel = ToDoModel("test model")
val results = mutableListOf<List<ToDoModel>>()
val itemsJob = launch {
underTest.items().collect { results.add(it) }
}
assertThat(results[0], empty())
underTest.save(testModel)
assertThat(results[1], contains(testModel))
underTest.delete(testModel)
assertThat(results[2], empty())
itemsJob.cancel()
}
}
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.