简体   繁体   中英

How can I test ORMLite with Roboelectric in an Android environment?

I am developing an Android app with Android Studio, using Gradle as build tool. Integration with Android Studio and Robolectric is done with android-unit-test gradle plugin .

I am also using ORMLite (latest version).

Is there a way I can test ORMLite DAO's with Robolectric 2.3-SNAPSHOT?

In such version some shadows were dropped, include SQLite ones (see Robolectric dropping SQLite shadows commit ). I looked at some unit tests in Robolectric repo, like this , but I need some information on how to setup the interaction with ORMLite.

Here is a working example I was able to compile and run.

@Config(emulateSdk = 18) // because Robolectric does not yet support API Level 19
@RunWith(RobolectricTestRunner.class)
public class DatabaseHelperManagerTest extends BaseTest {

    private Context context;

    public void initContext() {
        context = Robolectric.application.getApplicationContext();
        assertNotNull(context);
    }

    @BeforeClass
    public static void setup(){
        // To redirect Robolectric to stdout
        System.setProperty("robolectric.logging", "stdout");
    }

    @Test
    public void getDatabaseHelperTest(){
        initContext();

        DatabaseHelperManager databaseHelperManager = new DatabaseHelperManager();
        DatabaseHelper databaseHelper = databaseHelperManager.getHelper(context);
        assertNotNull(databaseHelper);

        Log.d(DatabaseHelperManagerTest.class.getName(), "Database Path:" + context.getDatabasePath(DatabaseHelper.DATABASE_NAME));
    }
}

For reference (in the DatabaseHelperManager class):

public DatabaseHelper getHelper(Context context) {
    return OpenHelperManager.getHelper(context, DatabaseHelper.class);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM