我目前正在为我的应用程序编写一个 csv 文件导入器,但我很难为其编写测试。我想要做的是导入一个示例 csv 文件并将结果与数据库进行比较。
public class CSVImportTest extends ProviderTestCase2<MyProvider> {
@Override
protected void setUp() throws Exception {
super.setUp();
mContentResolver = getMockContentResolver();
setContext(new IsolatedContext(mContentResolver, getContext()));
mContext = getContext();
mCSVImport = new CSVImportParker(mContext);
}
public void read() {
try {
// Fails here with "File not found."
InputStream input = mContext.getResources()
.openRawResource(my.package.R.raw.file);
...
} catch (Exception e) {
e.printStackTrace();
fail();
}
...
}
}
虽然在正确的位置可用,但从未找到测试文件。
最佳答案
问题是 raw 目录中的资源被压缩,除非它们具有 ogg 或 mp3 文件扩展名。请参阅此说明:
Proguard breaking audio file in assets or raw
来自 docs
This function only works for resources that are stored in the package as uncompressed data, which typically includes things like mp3 files and png images.
因此,解决此问题的最简单方法是将 mp3 或 ogg 文件扩展名添加到您的原始 Assets 中。它不干净也不漂亮,但它有效。
关于在 ProviderTestCase2 中找不到 Android 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15763399/