我想测试一个管理数据并使用带有ContentProvider
的自定义Robolectric
的类。
我制作了一个包含两个测试的测试类,这些测试使用ShadowContentResolver
来设置ContentProvider
中的某些数据。这些测试可以完美地自己完成,但是如果我测试整个课程,则在第二堂课中会出错:
java.lang.IllegalStateException: Illegal connection pointer 1. Current pointers for thread Thread[main,5,main] []
我的测试班:
@RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class, sdk = [27], manifest = "AndroidManifest.xml", application = TestApp::class)
class HomeSliderDatabaseTest {
private lateinit var homeSliderDatabase: HomeSliderDatabase
@Mock
private lateinit var contentResolver: ContentResolver
private lateinit var shadowContentResolver: ShadowContentResolver
@Before
fun setup() {
contentResolver = RuntimeEnvironment.application.contentResolver
shadowContentResolver = shadowOf(contentResolver)
homeSliderDatabase = HomeSliderDatabase(contentResolver)
}
@Test
fun determineIdsToFetch_success() {
// put a record in the content provider
shadowContentResolver.insert(CONTENT_URI, ContentValues().apply {
put(ID, 1)
put(TIMESTAMP, 1_000_000L)
})
val ids = homeSliderDatabase.determineIdsToFetch(mapOf(1 to 1_000L, 2 to 3_000_000L))
assert(ids.contains(2))
assertFalse(ids.contains(1))
}
@Test
fun deleteAllHomeSliderItems_success() {
shadowContentResolver.insert(CONTENT_URI, ContentValues().apply {
put(ID, 2)
put(TIMESTAMP, 1_000_000L)
})
val cursorBefore = shadowContentResolver.query(CONTENT_URI, null, null, null, null)
assert(cursorBefore.count > 0)
cursorBefore.close()
homeSliderDatabase.removeAllHomeSliderItems()
val cursorAfter = shadowContentResolver.query(CONTENT_URI, null, null, null, null)
assert(cursorAfter.count == 0)
cursorAfter.close()
}
@After
fun tearDown() {
}
}
这是完整的堆栈跟踪:
java.lang.IllegalStateException: Illegal connection pointer 1. Current pointers for thread Thread[main,5,main] []
at org.robolectric.shadows.ShadowSQLiteConnection$Connections.getConnection(ShadowSQLiteConnection.java:366)
at org.robolectric.shadows.ShadowSQLiteConnection$Connections.getStatement(ShadowSQLiteConnection.java:375)
at org.robolectric.shadows.ShadowSQLiteConnection$Connections.executeStatementOperation(ShadowSQLiteConnection.java:704)
at org.robolectric.shadows.ShadowSQLiteConnection$Connections.resetStatementAndClearBindings(ShadowSQLiteConnection.java:686)
at org.robolectric.shadows.ShadowSQLiteConnection.nativeResetStatementAndClearBindings(ShadowSQLiteConnection.java:297)
at android.database.sqlite.SQLiteConnection.nativeResetStatementAndClearBindings(SQLiteConnection.java)
at android.database.sqlite.SQLiteConnection.releasePreparedStatement(SQLiteConnection.java:916)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:520)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.__constructor__(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1546)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1417)
at my.company.contentprovider.HomeSliderContentProvider.insert(HomeSliderContentProvider.java:147)
at org.robolectric.shadows.ShadowContentResolver.insert(ShadowContentResolver.java:152)
at my.company.tests.HomeSliderDatabaseTest.determineIdsToFetch_success(HomeSliderDatabaseTest.kt:53)
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 org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:523)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:226)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:108)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:35)
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.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:62)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)