简体   繁体   English

Robolectric:运行多个测试失败

[英]Robolectric: running multiple tests fails

I am trying to run multiple tests with Robolectric 3.0 + Gradle using SQLite (OpenHelper) as database. 我正在尝试使用SQLite(OpenHelper)作为数据库,使用Robolectric 3.0 + Gradle运行多个测试。 Running each single tests works fine, but starting the whole test suite always results in RuntimeException in the 2nd test. 运行每个单独的测试工作正常,但启动整个测试套件总是导致第二次测试中的RuntimeException。

This is my test dummy, which does not work. 这是我的测试假人,不起作用。

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class Dummy {


    @Before
    public void setUp() throws Exception {
         // setup activity ...
    }

    @Test
    public void testA() throws Exception {
        Assert.assertTrue(true);
    }

    @Test
    public void testB() {
        Assert.assertTrue(true);
    }
}

Exception 例外

java.lang.RuntimeException: java.lang.IllegalStateException: Illegal connection pointer 1. Current pointers for thread Thread[pool-4-thread-1,5,main] []
    at org.robolectric.shadows.ShadowSQLiteConnection$Connections.execute(ShadowSQLiteConnection.java:470)
    at org.robolectric.shadows.ShadowSQLiteConnection.nativeResetStatementAndClearBindings(ShadowSQLiteConnection.java:286)
    at android.database.sqlite.SQLiteConnection.nativeResetStatementAndClearBindings(SQLiteConnection.java)
    at android.database.sqlite.SQLiteConnection.releasePreparedStatement(SQLiteConnection.java:915)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:519)
    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:1469)
    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
    at de.d360.android.sdk.v2.storage.db.datasource.AbstractDataSource.insert(AbstractDataSource.java:78)
    at de.d360.android.sdk.v2.storage.db.datasource.QueueMessageDataSource.create(QueueMessageDataSource.java:100)
    at de.d360.android.sdk.v2.net.Queue.addToHttpQueue(Queue.java:185)
    at de.d360.android.sdk.v2.D360Events.sendEvent(D360Events.java:1636)
    at de.d360.android.sdk.v2.D360Events.sendEvent(D360Events.java:1612)
    at de.d360.android.sdk.v2.D360Events.sendEvent(D360Events.java:1651)
    at de.d360.android.sdk.v2.D360Events.appInstanceUpdated(D360Events.java:359)
    at de.d360.android.sdk.v2.crm.AppInstanceUpdater.sendUpdateEvent(AppInstanceUpdater.java:27)
    at de.android.hotel.HotelApplication.onCreate(HotelApplication.java:281)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    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.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    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:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.IllegalStateException: Illegal connection pointer 1. Current pointers for thread Thread[pool-4-thread-1,5,main] []
    at org.robolectric.shadows.ShadowSQLiteConnection$Connections.getConnection(ShadowSQLiteConnection.java:333)
    at org.robolectric.shadows.ShadowSQLiteConnection$Connections.getStatement(ShadowSQLiteConnection.java:340)
    at org.robolectric.shadows.ShadowSQLiteConnection.stmt(ShadowSQLiteConnection.java:52)
    at org.robolectric.shadows.ShadowSQLiteConnection.access$000(ShadowSQLiteConnection.java:33)
    at org.robolectric.shadows.ShadowSQLiteConnection$16.call(ShadowSQLiteConnection.java:289)
    at org.robolectric.shadows.ShadowSQLiteConnection$Connections$6.call(ShadowSQLiteConnection.java:452)
    at org.robolectric.shadows.ShadowSQLiteConnection$Connections$6.call(ShadowSQLiteConnection.java:446)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

java.lang.RuntimeException: java.lang.RuntimeException: java.lang.IllegalStateException: Illegal connection pointer 1. Current pointers for thread Thread[pool-4-thread-1,5,main] []
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:244)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    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.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    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:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.RuntimeException: java.lang.IllegalStateException: Illegal connection pointer 1. Current pointers for thread Thread[pool-4-thread-1,5,main] []
    at org.robolectric.shadows.ShadowSQLiteConnection$Connections.execute(ShadowSQLiteConnection.java:470)
    at org.robolectric.shadows.ShadowSQLiteConnection.nativeResetStatementAndClearBindings(ShadowSQLiteConnection.java:286)
    at android.database.sqlite.SQLiteConnection.nativeResetStatementAndClearBindings(SQLiteConnection.java)
    at android.database.sqlite.SQLiteConnection.releasePreparedStatement(SQLiteConnection.java:915)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:519)
    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:1469)
    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
    at de.d360.android.sdk.v2.storage.db.datasource.AbstractDataSource.insert(AbstractDataSource.java:78)
    at de.d360.android.sdk.v2.storage.db.datasource.QueueMessageDataSource.create(QueueMessageDataSource.java:100)
    at de.d360.android.sdk.v2.net.Queue.addToHttpQueue(Queue.java:185)
    at de.d360.android.sdk.v2.D360Events.sendEvent(D360Events.java:1636)
    at de.d360.android.sdk.v2.D360Events.sendEvent(D360Events.java:1612)
    at de.d360.android.sdk.v2.D360Events.sendEvent(D360Events.java:1651)
    at de.d360.android.sdk.v2.D360Events.appInstanceUpdated(D360Events.java:359)
    at de.d360.android.sdk.v2.crm.AppInstanceUpdater.sendUpdateEvent(AppInstanceUpdater.java:27)
    at de.android.hotel.HotelApplication.onCreate(HotelApplication.java:281)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    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.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    ... 1 more
Caused by: java.lang.IllegalStateException: Illegal connection pointer 1. Current pointers for thread Thread[pool-4-thread-1,5,main] []
    at org.robolectric.shadows.ShadowSQLiteConnection$Connections.getConnection(ShadowSQLiteConnection.java:333)
    at org.robolectric.shadows.ShadowSQLiteConnection$Connections.getStatement(ShadowSQLiteConnection.java:340)
    at org.robolectric.shadows.ShadowSQLiteConnection.stmt(ShadowSQLiteConnection.java:52)
    at org.robolectric.shadows.ShadowSQLiteConnection.access$000(ShadowSQLiteConnection.java:33)
    at org.robolectric.shadows.ShadowSQLiteConnection$16.call(ShadowSQLiteConnection.java:289)
    at org.robolectric.shadows.ShadowSQLiteConnection$Connections$6.call(ShadowSQLiteConnection.java:452)
    at org.robolectric.shadows.ShadowSQLiteConnection$Connections$6.call(ShadowSQLiteConnection.java:446)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

Already tried some solutions like reseting singleton with the following code, which ends in FieldNotfoundException or NullPointerException, so did not work. 已经尝试了一些解决方案,比如使用以下代码重置单例,这些代码以FieldNotfoundException或NullPointerException结束,因此无效。

@After
public void finishComponentTesting() {
    // sInstance is the static variable name which holds the singleton instance
    resetSingleton(MySQLiteOpenHelper.class, "sInstance");
}

private void resetSingleton(Class clazz, String fieldName) {
    Field instance;
    try {
        instance = clazz.getDeclaredField(fieldName);
        instance.setAccessible(true);
        instance.set(null, null);
    } catch (Exception e) {
        throw new RuntimeException();
    }
}

Edit: fixed it by removing sendUpdateEvent() of D360Sdk's AppInstanceUpdater. 编辑:通过删除D360Sdk的AppInstanceUpdater的sendUpdateEvent()来修复它。

AppInstanceUpdater updater = D360Sdk.getCrmAppInstanceUpdater();
updater.setCustomId(Util.installId());
// updater.sendUpdateEvent();

It is an old question but maybe this solution can help someone as I was facing similar issue (exactly same error). 这是一个老问题,但也许这个解决方案可以帮助某人,因为我面临着类似的问题(完全相同的错误)。

The issue is that while running multiple test cases which are accessing database multiple times, the first test case opens the connection to the database and does not close it. 问题是,在运行多次访问数据库的多个测试用例时,第一个测试用例打开与数据库的连接,但不关闭它。 When second test case tries to open database connection again it fails. 当第二个测试用例再次尝试打开数据库连接时,它将失败。

I read some solutions and finally figured out that the issue was with opening database multiple times. 我读了一些解决方案,最后发现问题是多次打开数据库。 So to run tests successfully, do the following configuration in your TestCase.java file (where you have written your test cases): 因此,要成功运行测试,请在TestCase.java文件中执行以下配置(您已编写测试用例):

@Before
public void setUp() throws Exception {
    //Get an instance of your implementation of SQLiteOpenHelper class.
    //Let's assume the class name is MySQLiteHelper which extends SQLiteOpenHelper and has a function called getInstance
    //which returns the instance of the SQLiteOpenHelper.
    //Store this instance in a global variable in your TestCase.java file.
    databaseHelper = MySQLiteHelper.getInstance();
}

@After
public void tearDown() throws Exception {
    //use the instance created in setUp() function to close the database
    databaseHelper.close();
}

The above two functions annotated with "@Before" and "@After" run before and after every test case. 在每个测试用例之前和之后运行上面两个用“@Before”和“@After”注释的函数。 So, for after each test case we should close the database connection. 因此,对于每个测试用例之后,我们应该关闭数据库连接。

This and this links helped. 这个链接有所帮助。

Please comment if there is something wrong in the solution or if my understanding of this error is wrong. 如果解决方案中存在问题或者我对此错误的理解是错误的,请评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行Robolectric单元测试失败 - Running Robolectric unit tests fails Robolectric测试在Android Studio中运行,但不在命令行上运行 - Robolectric tests running in Android Studio but not on the command line 通过robolectric插件运行android单元测试 - Running android unit tests via robolectric plugin 具有AndroidAnnotations的Robolectric使测试挂起/未运行 - Robolectric with AndroidAnnotations make tests hang/not running 运行时异常运行robolectric测试IntelliJ - Runtime Exception running robolectric tests IntelliJ 无法使用Robolectric 4.0.1运行测试,&#39;packageDebugUnitTestForUnitTest&#39;失败 - Can't run tests with Robolectric 4.0.1, 'packageDebugUnitTestForUnitTest' fails 在Android Studio中运行Robolectric测试时如何调试? - How to debug when running Robolectric tests in Android Studio? Android单元测试-使用Robolectric运行MockServer时出现IncompatibleClassChangeError - Android unit tests - IncompatibleClassChangeError when running MockServer with Robolectric 使用 Maven surefire 插件运行 Robolectric 测试时出现 ClassNotFoundException - Getting ClassNotFoundException when running Robolectric tests with the Maven surefire plugin 使用Robolectric运行JUnit测试时出现Gradle和Android Studio问题 - Gradle and Android Studio issue when running JUnit tests using Robolectric
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM