我已經完成了這項工作(以一種不太優雅的方式),是將測試夾具複製到我的設備(或模擬設備)上。我將它命名爲「cleantestdatabase.db」。然後在測試代碼中,我將它複製到「testdatabase.db」,以便我可以用我的測試修改它,但將其重置爲已知狀態。這裏的代碼:
copyFile("cleantestdatabase.db", "testdatabase.db");
private void copyFile(String source, String dest) throws IOException{
String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + getActivity().getString(R.string.default_dir);
File newDir = new File(rootPath);
boolean result = newDir.mkdir();
if(result == false){
Log.e("Error", "result false");
}
InputStream in = new FileInputStream(rootPath + source);
File outFile = new File(rootPath + dest);
if(outFile.exists()) {
outFile.delete();
}
outFile.createNewFile();
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
非常感謝您的好評。我發現我仍然需要了解更多關於Android測試的知識。當我能夠實施其中一種解決方案時,我會更新你。 – 2010-10-07 02:15:23
當我嘗試在我的測試應用程序中執行= getResources()。getAssets()。open(「test.txt」)中的InputStream,並在我的資產目錄中放置名爲「test.txt」的文件時,出現異常。 – 2010-10-16 04:58:44
@glenviewjeff在什麼情況下你調用getResources()?在測試方法中還是在測試中啓動的活動中?您確定您使用的'Resources'來自Instrumentation的getContext()而不是'getTargetContext()'嗎? – 2010-10-17 02:12:14