Robolectric: running multiple tests fails Robolectric: running multiple tests fails android android

Robolectric: running multiple tests fails


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):

@Beforepublic 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();}@Afterpublic 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. 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.