I have DatabaseManager Class as below
public class DatabaseManager {
private AtomicInteger mOpenCounter = new AtomicInteger();
private static DatabaseManager instance;
private static MySQLiteOpenHelper mDatabaseHelper;
private SQLiteDatabase mDatabase;
private static final String DB_NAME = "KVStore";
private static final String TABLE_NAME = "master";
public static final String COLUMN_KEY = "Key";
public static final String COLUMN_VALUE ="Value";
private static final int DB_VERSION = 1;
//Initialize DBManager Instance
public static synchronized void initializeInstance(MySQLiteOpenHelper helper) {
if (instance == null) {
instance = new DatabaseManager();
mDatabaseHelper = helper;
}
}
//Get DBManager Instance
public static synchronized DatabaseManager getInstance() {
if (instance == null) {
throw new IllegalStateException(DatabaseManager.class.getSimpleName() +
" is not initialized, call initializeInstance(..) method first.");
}
return instance;
}
//Open DB for Read/Write
public synchronized SQLiteDatabase openDatabase() {
if(mOpenCounter.incrementAndGet() == 1) {
// Opening new database
mDatabase = mDatabaseHelper.getWritableDatabase();
}
return mDatabase;
}
//Close DB
public synchronized void closeDatabase() {
if(mOpenCounter.decrementAndGet() == 0) {
// Closing database
mDatabase.close();
}
}
// MySQLiteOpenHelper
public static class MySQLiteOpenHelper extends SQLiteOpenHelper {
public MySQLiteOpenHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table " + TABLE_NAME +"("+COLUMN_KEY+" TEXT primary key,"+COLUMN_VALUE+" TEXT )");
}
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldv, int newv) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
onCreate(sqLiteDatabase);
}
}
}
And KVStoreWrapper class as below:
public class KVStoreWrapper {
static SQLiteDatabase database;
static String key,value;
//Add new Key, Value entry
public void addEntry(String key,String value)
{
database = DatabaseManager.getInstance().openDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("Key",key);
contentValues.put("Value",value);
database.insertOrThrow("master", null, contentValues);
DatabaseManager.getInstance().closeDatabase();
}
public void addIntEntry(String key,int value)
{
database = DatabaseManager.getInstance().openDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("Key",key);
contentValues.put("Value",value);
database.insertOrThrow("master", null, contentValues);
DatabaseManager.getInstance().closeDatabase();
}
}
And my instrumented test class is below:
import android.content.Context;
import android.os.Handler;
import android.support.test.runner.AndroidJUnit4;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class KVStoreTest extends InstrumentationTestCase {
private TestActivity ha;
private KVStoreWrapper kvsw;
Handler mHandler;
@Test
public void testAddRetrieveEntry() {
Context context = this.getInstrumentation().getTargetContext().getApplicationContext();
DatabaseManager.MySQLiteOpenHelper myDBHelper=new DatabaseManager.MySQLiteOpenHelper(context);
DatabaseManager.initializeInstance(myDBHelper);
kvsw=new KVStoreWrapper();
String key="Key";
String value="Value";
kvsw.addEntry(key, value);
String retrievedValue=kvsw.retrieveEntry(key);
assertEquals("Conversion from celsius to fahrenheit failed",value,
retrievedValue);
}
}
But I get error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
What is correct way to get activity context to test sqlite based api's.
Aucun commentaire:
Enregistrer un commentaire