我有以下 DatabaseHander 类,它可以存储用户从Android手机输入的一些数据。即球员的名字和球员的评分:
public class DatabaseHandler extends SQLiteOpenHelper {
private final ArrayList<LastGameModel> lastGameModelsList = new ArrayList<>();
public DatabaseHandler(Context context) {
super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_MOVIES_TABLE = "CREATE TABLE " + Constants.TABLE_NAME +
"(" + Constants.KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , " + Constants.PLAYER_NAME +
" TEXT, " + Constants.PLAYER_RATE + " INTEGER);";
db.execSQL(CREATE_MOVIES_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_NAME);
onCreate(db);
}
//add content to table
public void addMovies(LastGameModel lastGameModel) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put(Constants.KEY_ID,lastGameModel.getId());
values.put(Constants.PLAYER_NAME, lastGameModel.getPlayerName());
values.put(Constants.PLAYER_RATE, lastGameModel.getPlayerRate());
db.insert(Constants.TABLE_NAME, null, values);
db.close();
Log.d("Theo","heeeey!data saved");
}
//get all folders
public ArrayList<LastGameModel> getBestPlayersDetails() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(Constants.TABLE_NAME, new String[]{
Constants.KEY_ID, Constants.PLAYER_NAME,Constants.PLAYER_RATE}, null, null, null, null, Constants.KEY_ID + " DESC");
if (cursor.moveToFirst()) {
do {
LastGameModel lgm = new LastGameModel();
lgm.setPlayerName(cursor.getString(cursor.getColumnIndex(Constants.PLAYER_NAME)));
lgm.setPlayerRate(cursor.getInt(cursor.getColumnIndex(Constants.PLAYER_RATE)));
lgm.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Constants.KEY_ID))));
lastGameModelsList.add(lgm);
}while(cursor.moveToNext());
}
return lastGameModelsList;
}
public void deletePlayer(int id){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Constants.TABLE_NAME,Constants.KEY_ID + " = ? ",new String[]
{String.valueOf(id)});
lastGameModelsList.clear();
db.close();
}
}
现在我想用JUNIT 4测试上面的类。
所以:
@RunWith(JUnit4.class)
public class DBTesting {
public static String playerName;
public static String playerRate;
public static long playerId;
Context mMockContext;
@Test
public void testDropDB() {
assertTrue(mMockContext.deleteDatabase(Constants.DATABASE_NAME));
}
}
但是所有测试都失败了
testDropDB()正在给我
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean
android.content.Context.deleteDatabase(java.lang.String)' on a null object
reference
at team.football.ael.DBTesting.deleteTheDatabase(DBTesting.java:33)
at team.football.ael.DBTesting.testDropDB(DBTesting.java:30)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
为什么会这样? Constants.DATABASE_NAME不是空字符串。
public class Constants implements BaseColumns{
public static final String DATABASE_NAME="ratedplayersdb";
public static final int DATABASE_VERSION=2;
public static final String TABLE_NAME="ratedplayers";
public static final String PLAYER_NAME="playername";
public static final String PLAYER_RATE="player_rate";
public static final String KEY_ID="_id";
}
答案 0 :(得分:0)
mMockContext
为null,如错误消息中所述