I would like to do some Unit test in Android for SQLiteOpenHelper using Robolelectric. However, I can't create the database as I can't get the context. I tried out 4 options that are mentioned here How can we access context of an application in Robolectric? but none of them worked. Here you see the code of the testclass and in the "setUp" method you see the different options that I tried with the corresponding error message after //
package com.example.td.barapp;
import android.content.Context;
import android.database.Cursor;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class DataBaseHelperTest {
private DataBaseHelper helperDB;
@Before
public void setUp() throws Exception {
// Context context = ShadowApplication.getInstance().applicationContext; //Error: Cannot resolve symbol 'applicationContext'
//Context context = Robolectric.application; // Error: Cannot resolve symbol application
//Context context = ApplicationProvider.getApplicationContext(); // Error: Cannot resolve symbol 'ApplicationProvider'
//Context context = RuntimeEnvironment.systemContext; // Error: Cannot resolve symbol 'systemContext'
//Context context = getClass().getClassLoader(); // Error: Required type: Context Provided: ClassLoader
Context context = InstrumentationRegistry.getInstrumentation().getContext(); // Error: Cannot resolve symbol 'InstrumentationRegistry'
helperDB = new DataBaseHelper(context);
}
@After
public void tearDown() {
helperDB.close();
}
@Test
public void insertDataDB_TableRatings() {
boolean insertRating = helperDB.insertDataDB_TableRatings("Orange Juice", "Any", "Any", 1,
0,0,1,0,0 );
Assert.assertTrue(insertRating);
}
@Test
public void getDataDB_TableRating() {
String drinkName = "Orange Juice";
String drinkStrength = "Any";
String drinkSize = "Any";
boolean insertRating = helperDB.insertDataDB_TableRatings(drinkName, drinkStrength, drinkSize, 1,
0,0,1,0,0 );
Cursor res = helperDB.getDataDB_TableRating(drinkName, drinkStrength, drinkSize);
Assert.assertFalse(res.getCount()==0);
res.moveToFirst();
Assert.assertEquals (res.getString(0), drinkName);
Assert.assertEquals (res.getString(6), "1");
drinkSize = "Large";
boolean insertRating2 = helperDB.insertDataDB_TableRatings(drinkName, drinkStrength, drinkSize, 1,
0,0,0,1,0 );
res = helperDB.getDataDB_TableRating(drinkName, drinkStrength, drinkSize);
Assert.assertFalse(res.getCount()==0);
res.moveToFirst();
Assert.assertEquals (res.getString(2), drinkName);
Assert.assertEquals (res.getString(7), "1");
}
@Test
public void updateDataDB_TableRatings() {
String drinkName = "Orange Juice";
String drinkStrength = "Any";
String drinkSize = "Any";
boolean insertRating = helperDB.insertDataDB_TableRatings(drinkName, drinkStrength, drinkSize, 1, 0,0,1,0,0 );
boolean valueUpdated = helperDB.updateDataDB_TableRatings(drinkName, drinkStrength , drinkSize, 2, 0, 0, 0, 1, 0);
Cursor res = helperDB.getDataDB_TableRating(drinkName, drinkStrength, drinkSize);
Assert.assertFalse(res.getCount()==0);
res.moveToFirst();
Assert.assertEquals (res.getString(8), "1");
}
@Test
public void deleteDataDB_TableRatings() {
String drinkName = "Orange Juice";
String drinkStrength = "Any";
String drinkSize = "Large";
boolean insertRating = helperDB.insertDataDB_TableRatings(drinkName, drinkStrength, drinkSize, 1, 0,0,1,0,0 );
helperDB.deleteDataDB_TableRatings (drinkName, drinkStrength, drinkSize);
}
}
I also added the following lines in my build.gradle file:
testImplementation 'junit:junit:4.13'
testImplementation 'org.robolectric:robolectric:3.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Would you mind telling me what mistake I am making? I'd appreciate every comment.
Update: As it seems that I might be missing some dependencies I uploaded the build.gradle file with the dependencies. Would you mind telling me what the mistake is and why I can't get any context with Roblectric?
apply plugin: 'com.android.application'
apply plugin: "androidx.navigation.safeargs"
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.td.barapp"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
viewBinding {
enabled = true
}
dataBinding {
enabled=true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
def nav_version = "2.3.0"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.2.0-alpha06'
testImplementation 'junit:junit:4.13'
testImplementation 'org.robolectric:robolectric:3.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'android.arch.core:core-testing:1.2.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation 'androidx.cardview:cardview:1.0.0'
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
}