Я пытаюсь написать модульный тест для своей базы данных, но получаю следующую ошибку.Ошибка говорит о том, что не должно быть значение null
.Через Log.i
я могу узнать, является ли переменная count
значением null
.Но я не знаю, почему переменная count
стоила null
.
I/Junit: createDb
I/Junit: createAndRetriveNotes: count= null 3
I/Junit: closeDb
----- begin exception -----
E/TestRunner: java.lang.AssertionError: expected:<3> but was:<null>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.riluq.notesapp.DatabaseTest.createAndRetriveDatabase(DatabaseTest.kt:47)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:388)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2090)
----- end exception -----
java.lang.AssertionError: expected:<3> but was:<null>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.riluq.notesapp.DatabaseTest.createAndRetriveDatabase(DatabaseTest.kt:47)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:388)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2090)
Я проверил свои sql запросы в NoteDao.kt, но похоже, что проблема не в этом.
DatabaseTest.kt
import android.content.Context
import android.util.Log
import androidx.room.Room
import androidx.test.InstrumentationRegistry
import androidx.test.runner.AndroidJUnit4
import com.riluq.notesapp.database.AppDatabase
import com.riluq.notesapp.database.NoteDao
import com.riluq.notesapp.util.getNotes
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
@RunWith(AndroidJUnit4::class)
class DatabaseTest {
companion object{
val TAG: String = "Junit"
}
private var mDb: AppDatabase? = null
private var mDao: NoteDao? = null
@Before
fun createDb() {
val context: Context = InstrumentationRegistry.getTargetContext()
mDb = Room.inMemoryDatabaseBuilder(context,
AppDatabase::class.java).build()
Log.i(TAG, "createDb")
}
@After
fun closeDb() {
mDb?.close()
Log.i(TAG, "closeDb")
}
@Test
fun createAndRetriveDatabase() {
mDao?.insertNoteAll(getNotes())
val count: Int? = mDao?.getCount()
Log.i(TAG, "createAndRetriveNotes: count= $count " + getNotes().size )
assertEquals(getNotes().size, count)
}
}
AppDatabase.kt
import android.content.Context
import androidx.room.*
@Database(entities = [NoteEntity::class], version = 1, exportSchema = false)
@TypeConverters(DateConverter::class)
abstract class AppDatabase: RoomDatabase() {
abstract fun noteDao(): NoteDao
companion object {
val DATABASE_NAME: String = "AppDatabase.db"
private var INSTANCE: AppDatabase? = null
fun getInstance(context: Context): AppDatabase? {
if (INSTANCE == null) {
synchronized(AppDatabase::class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.applicationContext,
AppDatabase::class.java, DATABASE_NAME)
.build()
}
}
}
return INSTANCE
}
fun destroyInstance() {
INSTANCE = null
}
}
}
NoteDao.kt
import androidx.lifecycle.LiveData
import androidx.room.*
import androidx.room.OnConflictStrategy.REPLACE
@Dao
interface NoteDao {
@Insert(onConflict = REPLACE)
fun insertNote(noteEntity: NoteEntity)
@Insert(onConflict = REPLACE)
fun insertNoteAll(notes: List<NoteEntity>)
@Delete
fun deleteNote(noteEntity: NoteEntity)
@Query("SELECT * FROM notes WHERE id = :id")
fun getNoteById(id: Int): NoteEntity
@Query("SELECT * FROM notes ORDER BY date DESC")
fun getAll(): LiveData<List<NoteEntity>>
@Query("DELETE FROM notes")
fun deleteAll(): Int
@Query("SELECT COUNT(*) FROM notes")
fun getCount(): Int
}
NoteEntity.kt
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.*
@Entity(tableName = "notes")
data class NoteEntity(
@PrimaryKey(autoGenerate = true)
var id: Int?,
var date: Date?,
var text: String
){
constructor():this(null, null, "")
}
Почему переменная count
в DatabaseTest.kt
имеет значение null
?и как мне это решить?