【发布时间】:2021-02-05 14:02:59
【问题描述】:
我正在我的 android 应用程序中进行一些测试。
该应用程序使用 Room 进行数据存储,现在我正在编写一些测试来测试数据访问对象功能。
我的问题是我每次不能运行多个测试,因为它给了我这个错误:Cannot perform this operation because the connection pool has been closed。
我在互联网上找不到任何解决方案。
这是我的测试课:
@RunWith(RobolectricTestRunner::class)
@Config(maxSdk = Build.VERSION_CODES.P, minSdk = Build.VERSION_CODES.P)
class MeteofyDatabaseTest {
@get:Rule
val testRule = InstantTaskExecutorRule()
private lateinit var dao: WeatherPlaceDAO
private lateinit var appContext : Context
private lateinit var db : MeteofyDatabase
@Before
fun setup() {
appContext = InstrumentationRegistry.getInstrumentation().targetContext
MeteofyDatabase.TEST_MODE = true
db = MeteofyDatabase.getDatabaseInstance(appContext)
dao = db.weatherPlaceDAO()
}
@After
@Throws(IOException::class)
fun tearDown() {
db.close()
}
@Test
fun insertAndRetrieveData() {
val weatherPlace = WeatherPlace("test_place", "10", "Clouds")
val testObserver = dao.getWeatherPlacesAsLiveData().test()
dao.insertNewPlace(weatherPlace)
Assert.assertTrue(testObserver.value()[0].placeName == weatherPlace.placeName)
}
//THIS GIVE ME THE ERROR
@Test
fun insertAndDeleteData(){
val weatherPlace = WeatherPlace("test_place", "10", "Clouds")
val testObserver = dao.getWeatherPlacesAsLiveData().test()
dao.insertNewPlace(weatherPlace)
dao.deleteByPlaceId(weatherPlace.placeName)
Assert.assertTrue(testObserver.value().isEmpty())
}
}
【问题讨论】:
标签: android unit-testing kotlin junit android-room