我的代码:
abstract class DbTest {
@Rule
@JvmField
val countingTaskExecutorRule = CountingTaskExecutorRule()
private lateinit var _db : AppDatabase
val db: AppDatabase
get() = _db
@Before
fun initDb() {
_db = Room.inMemoryDatabaseBuilder(
InstrumentationRegistry.getInstrumentation().context,
AppDatabase::class.java
).build()
}
@After
fun closeDb() {
countingTaskExecutorRule.drainTasks(10, TimeUnit.SECONDS)
_db.close()
}
}
@RunWith(AndroidJUnit4::class)
class PlantDaoTest : DbTest() {
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Test
fun insert_one_plant() {
val plant = Plant(plantId = 1, name="Plant1")
db.plantDao.insertOnePlant(plant)
val retrievedPlant = db.plantDao.getPlant(1)
assert(plant.name==retrievedPlant.name)
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行 PlantDaoTest 时,我看到这个错误:kotlin.UninitializedPropertyAccessException: lateinit property _db has …