Take the 2-minute tour ×

anyone could help me with the problem: how to test android apps which uses ormlite?

I'd like to know how to create mocks form dao. This code gets me a dao

public Dao<Account, Integer> getAccountDao() throws SQLException {
    if (accountDao == null) {
        accountDao = getDao(Account.class);
    return accountDao;

My testing activity looks this:

public class OrmActivity extends OrmLiteBaseActivity<DatabaseHelper> {

    public void onCreate(Bundle savedInstanceState) {


        RuntimeExceptionDao<Account, Integer> accountDao = getHelper().getAccountDataDao();
        Account account = new Account();

I dont want to create separate database for tests. Instend of it I want to use mocks.

Best regards

share|improve this question
add comment

2 Answers

I use Robolectric for unit testing Android apps. Works fine with RoboGuice and Ormlite. Robolectric creates its own database, so a unit test doesn't modify the database on the device you are testing with.

If this doesn't answer your question, please clarify your question. Like, how is not using a separate database related to using mock dao's?

share|improve this answer
thx for reply, I will check it –  reizals Jun 30 '12 at 7:45
@christine - do you a sample code of roboelectric and ormlite working together –  gaurav Dec 18 '13 at 7:55
I put up sample code here: www.christine.nl –  Christine Dec 18 '13 at 9:45
add comment

Just in case anyone else stumbles across this post. The issue I had was that you need an instance of an Activity so you can pass the context to the OpenHelperManager.getHelper(android.content.Context context, Class openHelperClass) method.

To get around this I create a ActivityInstrumentationTestCase2 test class to provide the context. You could use MockContext but I understand it to be problematic. This way is simple stupid, sort of :)

At that point I don't bother testing any of the Activity functionality, just database functionality.

public class TestActivity extends ActivityInstrumentationTestCase2<MainFragment> {

    public TestActivity() {

    protected void setUp() throws Exception {
        //DatabaseHelper.class is the extended OrmLiteSqliteOpenHelper class
        OpenHelperManager.getHelper(getActivity(), DatabaseHelper.class);

    protected void tearDown() throws Exception {

    public void testDetailDiscount() {
    //Example code using an entity class that encapsulates ormlite functionality 
        double total = 95 * 5;
        DocumentDetail detail = DocumentDetail.create(getActivity());
        assertEquals(total, detail.getTotal());

Hope it helps

share|improve this answer
add comment

Your Answer


