我想测试databasehelper类中的方法。我用Android studio开发。我使用DatabaseHelper
进行测试,我收到运行测试的错误消息:
android.database.sqlite.SQLiteOpenHelper中的方法getReadableDatabase未被模拟。
这是我的public class DatabaseHelper2 extends SQLiteOpenHelper {
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.example.b.mywm/databases/";
private static String DB_NAME = "429";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor
*/
public DatabaseHelper2(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void openDataBase() throws SQLException {
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
@Override
public synchronized void close() {
if(myDataBase != null) {
myDataBase.close();
}
super.close();
}
public List<Topic> getTopics() {
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<Topic> topics = new ArrayList<Topic>();
int id = -1;
String description = "";
Cursor c = db.rawQuery("SELECT * FROM" + "Topics", null);
if (c.moveToFirst()) {
id = c.getInt(c.getColumnIndex("ID"));
description = c.getString(c.getColumnIndex("Description"));
Topic t = new Topic(id, description);
topics.add(t);
}
c.close();
// Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
// cursor.getString(1), cursor.getString(2));
// return contact
return topics;
}
}
课程:
@RunWith(MockitoJUnitRunner.class)
public class DBTest {
@Mock
Context mMockContext;
@Mock
DatabaseHelper2 dataBaseHelper;
@Before
public void setUp() {
dataBaseHelper = new DatabaseHelper2(mMockContext);
}
@Test
public void readStringFromContext_LocalizedString() {
List<Topic> f = dataBaseHelper.getTopics();
}
}
我的测试:
$(document).ready(function () {
$('[data-toggle="tooltip"]').tooltip();
// Locator
$("#textLocator").autocomplete({
source: function (request, response) {
$.ajax({
url: "/Home/AutocompleteSuggestions",
type: "POST",
dataType: "json",
data: { term: request.term },
success: function (data) {
response($.map(data, function (item) {
return {
value: item.name,
label : item.name
};
}))
}
})
},
messages: {
noResults: "", results: ""
}
});