I'm facing an Error while testing Room DataBase in Android. So can you please help Me?
I think this error is due to id 'kotlin-kapt'
in Build.gradle (app)
This is Build.Gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
dependencies {
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.5'
}
}
plugins {
id 'com.android.application' version '7.1.0' apply false
id 'com.android.library' version '7.1.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id 'androidx.navigation.safeargs.kotlin' version '2.4.0' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
This is Build.Gradle (app)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.example.learningunittesting"
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'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
def room_version = "2.4.1"
def lifecycle_version = "2.5.0-alpha01"
def arch_version = "2.1.0"
def kotlin_cor = "1.6.0"
def nav_version = "2.4.0"
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
//Default Android testing Dependency
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
// Google Truth Dependency
testImplementation "com.google.truth:truth:1.1.3"
// This is For UI testing
androidTestImplementation "com.google.truth:truth:1.1.3"
// Room
implementation "androidx.room:room-runtime:2.4.1"
kapt "androidx.room:room-compiler:2.4.1"
// Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:2.4.1"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0"
// Kotlin Coroutine
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'
//Kotlin testing dependency
//testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0'
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
// Testing View Model
testImplementation "androidx.arch.core:core-testing:$arch_version"
// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
//Dagger - Hilt
implementation "com.google.dagger:hilt-android:2.40.5"
kapt "com.google.dagger:hilt-android-compiler:2.35"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
}
This is My Room DataBase Dao Testing File
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.example.learningunittesting.uitls.getOrAwaitValue
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
@SmallTest
class ShoppingDaoTest {
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
private lateinit var database: ShoppingItemDatabase
private lateinit var dao: ShoppingDao
@Before
fun setup() {
database = Room.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(),
ShoppingItemDatabase::class.java
).allowMainThreadQueries().build()
dao = database.shoppingDao()
}
@After
fun teardown() {
database.close()
}
@Test
fun insertShoppingItem() = runTest {
val shoppingItem = ShoppingItem("name", 1, 1f, "url", id = 1)
dao.insertShoppingItem(shoppingItem)
val allShoppingItems = dao.observeAllShoppingItems().getOrAwaitValue()
assertThat(allShoppingItems).contains(shoppingItem)
}
@Test
fun deleteShoppingItem() = runTest {
val shoppingItem = ShoppingItem("name", 1, 1f, "url", id = 1)
dao.insertShoppingItem(shoppingItem)
dao.deleteShoppingItem(shoppingItem)
val allShoppingItems = dao.observeAllShoppingItems().getOrAwaitValue()
assertThat(allShoppingItems).doesNotContain(shoppingItem)
}
@Test
fun observeTotalPriceSum() = runTest {
val shoppingItem1 = ShoppingItem("name", 2, 10f, "url", id = 1)
val shoppingItem2 = ShoppingItem("name", 4, 5.5f, "url", id = 2)
val shoppingItem3 = ShoppingItem("name", 0, 100f, "url", id = 3)
dao.insertShoppingItem(shoppingItem1)
dao.insertShoppingItem(shoppingItem2)
dao.insertShoppingItem(shoppingItem3)
val totalPriceSum = dao.observeTotalPrice().getOrAwaitValue()
assertThat(totalPriceSum).isEqualTo(2 * 10f 4 * 5.5f)
}
}
CodePudding user response: