mardi 1 mars 2016

Writing SQLiteOpenHelper test cases in JUnit4 and Mockito

I am trying to write test cases for my database.

I have a helper class that extends to SQLiteOpenHelper

DBHelper.java
       public DBHelper(Context context) {
            super(context, DBConstants.DATABASE_NAME, null, DBConstants.DATABASE_VERSION);
        }

and a constructor class that has all the inserts deletes etc.

DBController.java
      public DBController open() throws SQLException {
            dbHelper = DBHelper.getInstance(context);
            database = dbHelper.getWritableDatabase();
            return this;
        }

my test class

DBControllerTest.java
@Mock
    Context mContext;
    DBController dbController;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        RenamingDelegatingContext context = new RenamingDelegatingContext(mContext, "test_");
        dbController = new DBController(context);
        dbController.open();
    }

Here when i do dbController.open(), the dbHelper.getWritableDatabase() always returns null.

How do i solve this problem. Also am I mocking it the right way. I have searched this a lot but did not find a solution. What is the best way to test database queries.

Aucun commentaire:

Enregistrer un commentaire