jeudi 28 mai 2015

Android Studio Unit test SQLiteDataBase is null

I'm new to Unit testing and I want to test my SQLiteDataBase.

I have a class named MySQLiteHelper that extends SQLiteOpenHelper. I have a class named LocationDataHandler that I use to add or delete elements from my DataBase. And I have a class LocationDataHandlerTest, that extends AndroidTestCase, to test my LocationDataHandler class.

Here is my MySQLiteHelper class :

public class MySQLiteHelper extends SQLiteOpenHelper {

    private static final String DATA_BASE_NAME = "MyDataBase";

    private static final int DATA_BASE_VERSION = 1;

    public static final String LOCATION_TABLE_NAME = "locations";

    public static final String COLUMN_ID = "_id";

    public static final String COLUMN_DATE = "_date";

    private static final String CREATE_LOCATION_TABLE = "create table " + LOCATION_TABLE_NAME + "(" + COLUMN_ID + " integer primary key autoincrement, "
            + COLUMN_DATE + " integer);";

    public MySQLiteHelper(Context context) {
        super(context, DATA_BASE_NAME, null, DATA_BASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        createLocationTable(db);
    }

    public void createLocationTable(SQLiteDatabase db) {
        db.execSQL(CREATE_LOCATION_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //nothing for now
    }

}

Here is my LocationDataHandler class :

public class LocationDataHandler {

    private SQLiteDatabase db;
    private MySQLiteHelper dbHelper;


    private String[] allColumns = {MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_DATE};


    public LocationDataHandler(Context context) {
        dbHelper = new MySQLiteHelper(context);
    }

    public void open() throws SQLException {
        db = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public void addLocation(int date) {

        ContentValues values = new ContentValues();

        values.put(MySQLiteHelper.COLUMN_DATE, date);

        db.insert(MySQLiteHelper.LOCATION_TABLE_NAME, null, values);
    }
}

And finally here is my LocationDataHandlerTest class :

@RunWith(MockitoJUnitRunner.class)
public class LocationDataHandlerTest extends AndroidTestCase {

    private LocationDataHandler tester;

    int date_1 = 123456789;
    int date_2 = 000000000;
    int date_3 = -123456789;

    @Before
    public void setUp() throws Exception {
        super.setUp();
    }

    @After
    public void tearDown() throws Exception {     
    }

    @Test
    public void testAddLocation() throws Exception {

        //Context context = new Activity();

        //RenamingDelegatingContext context = new RenamingDelegatingContext(getContext(), "test_");

        //IsolatedContext context = getMockContext();

        Context context = getContext();

        MySQLiteHelper helper = new MySQLiteHelper(context);

        assertNotNull(helper);

        SQLiteDatabase db = helper.getWritableDatabase();

        assertNotNull(db);

        ContentValues values = new ContentValues();

        values.put(MySQLiteHelper.COLUMN_DATE, date_1);

        db.insert(MySQLiteHelper.LOCATION_TABLE_NAME, null, values);

        Cursor cursor = db.query(MySQLiteHelper.LOCATION_TABLE_NAME,
                tester.getAllColumns(), null, null, null, null, null);

        cursor.moveToFirst();

        assertEquals(cursor.getInt(1), date_1);

        cursor.close();
    }
}

My problem is that the test is failing on the line "assertNotNull(db);" meaning i can never retrieve my dataBase. It's really weird since I have no problems doing it in my application.

As you can see I commented my attempts to provide my SQLiteOpenHelper with many contexts but none of them changed anything.

Am I doing this the wrong way ? Am I not using the right context ?

Aucun commentaire:

Enregistrer un commentaire