Android Sqlite Database Unit Testing
Solution 1:
One way to implement SQLite testing is with an instrumented unit test, using the InstrumentationRegistry in the Android test package to obtain a Context.
Here is an example from a tutorial and an example on GitHub:
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@RunWith(AndroidJUnit4.class)
@LargeTest
publicclassSQLiteTest {
private RateDataSource mDataSource;
@Before
publicvoidsetUp(){
mDataSource = new RateDataSource(InstrumentationRegistry.getTargetContext());
mDataSource.open();
}
@After
publicvoidfinish() {
mDataSource.close();
}
@Test
publicvoidtestPreConditions() {
assertNotNull(mDataSource);
}
@Test
publicvoidtestShouldAddExpenseType() throws Exception {
mDataSource.createRate("AUD", 1.2);
List<Rate> rate = mDataSource.getAllRates();
assertThat(rate.size(), is(1));
assertTrue(rate.get(0).toString().equals("AUD"));
assertTrue(rate.get(0).getValue().equals(1.2));
}
@Test
publicvoidtestDeleteAll() {
mDataSource.deleteAll();
List<Rate> rate = mDataSource.getAllRates();
assertThat(rate.size(), is(0));
}
@Test
publicvoidtestDeleteOnlyOne() {
mDataSource.createRate("AUD", 1.2);
List<Rate> rate = mDataSource.getAllRates();
assertThat(rate.size(), is(1));
mDataSource.deleteRate(rate.get(0));
rate = mDataSource.getAllRates();
assertThat(rate.size(), is(0));
}
@Test
publicvoidtestAddAndDelete() {
mDataSource.deleteAll();
mDataSource.createRate("AUD", 1.2);
mDataSource.createRate("JPY", 1.993);
mDataSource.createRate("BGN", 1.66);
List<Rate> rate = mDataSource.getAllRates();
assertThat(rate.size(), is(3));
mDataSource.deleteRate(rate.get(0));
mDataSource.deleteRate(rate.get(1));
rate = mDataSource.getAllRates();
assertThat(rate.size(), is(1));
}
}
Solution 2:
Try to find some tutorials, articles about unit testing SQLite database in Java. Unit Testing in Java and Android is certainly the same.
Although you would find these topics on Stack:
How to test Android sqlite with junit?
How to test methods that deal with SQLite database in android?
or this article:
Android Easy SQLite With Unit Tests
According to testing SQLite databese with Junit, check this:
Android JUnit test for SQLiteOpenHelper
where you would find this solution:
For a simple DatabaseHandler:
<!-- language: java --> publicclassMyDatabaseextendsSQLiteOpenHelper { privatestaticfinalStringDATABASE_NAME="database.db"; privatestaticfinalintDATABASE_VERSION=1; publicMyDatabase(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); } @OverridepublicvoidonCreate(SQLiteDatabase db){ // some code } @OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // some code } }
I created an AndroidTestCase:
<!-- language: java --> publicclassDatabaseTestextendsAndroidTestCase { private MyDatabase db; @OverridepublicvoidsetUp()throws Exception { super.setUp(); RenamingDelegatingContextcontext=newRenamingDelegatingContext(getContext(), "test_"); db = newMyDatabase(context); } @OverridepublicvoidtearDown()throws Exception { db.close(); super.tearDown(); } //According to Zainodis annotation only for legacy and not valid with gradle>1.1://@TestpublicvoidtestAddEntry(){ // Here i have my new database wich is not connected to the standard database of the App } }
Read also:
Solution 3:
This answer suggests the Roboelectric library. That worked for me. It's faster than an instrumented test, which has to run in an emulator.
Post a Comment for "Android Sqlite Database Unit Testing"