1

How is it possible to use a local (DB on file system) SQLiteDatabase in robolectric 2.3? All solutions on the Internet refer to robolectric < 2.3.

I want to do this, because robolectric does not find the created tables in a second or third test.

Oleksandr
  • 6,226
  • 1
  • 46
  • 54
elCapitano
  • 1,821
  • 3
  • 22
  • 42
  • Have looked at [this question](http://stackoverflow.com/questions/19690811/)? It explains how to use a fresh in-memory DB for each test case. Would that help? – DDD Jun 23 '14 at 13:04
  • Have you resolved this? If so, please choose an answer. – Jared Burrows May 18 '15 at 17:09

1 Answers1

1

I found a solution using a specific annotation:

import org.robolectric.util.DatabaseConfig.UsingDatabaseMap;
import org.robolectric.util.DatabaseConfig.DatabaseMap;
....
@RunWith(RobolectricTestRunner.class)
@UsingDatabaseMap(DbMap.class)
public class GenericTest{....}

The class DbMap must implement

class DbMap implements DatabaseMap
{
    private static final String localDb = "/Users/elcapitano/temp/myDb.db";


    public String getDriverClassName()
    {
        return "org.sqlite.JDBC";
    }


    public String getSelectLastInsertIdentity()
    {
        return "SELECT last_insert_rowid() AS id";
    }


    public int getResultSetType()
    {
        return ResultSet.TYPE_FORWARD_ONLY;
    }

    public String getConnectionString(File arg0)
    {
        return String.format("jdbc:sqlite:%s", localDb);
    }
}

Additionally the location of 'DbMap.localDb' was the same as during initializing the DB through 'SQLiteDatabase.openOrCreateDatabase(dbFile.getAbsolutePath(), null);'. Perhaps this is not relevant.

After this you can use a local file-DB in roboletric!

elCapitano
  • 1,821
  • 3
  • 22
  • 42