模拟 Android AssetManager [英] Mocking Android AssetManager
问题描述
我有一段代码接受 Context 并将此上下文传递给私有方法.私有方法调用 getAssets().open() 来读取我应用的资产文件夹中存在的文件.
I have a piece of code that accepts Context and passes this context to a private method. The private method calls the getAssets().open() to read a file present in the assets folder of my app.
public void methodA(Context ctx) throws IOException{
// do some stuff here...
Object data[] = getFileContents(ctx);
// use the data[] returned here...
}
private Object[] getFileContents(Context ctx) throws IOException{
Object[] data;
BufferedInputStream is = new BufferedInputStream(context.getAssets().open("test.txt"));
// parse file and create array of Objects[]
return data[];
}
我正在编写单元测试来使用 Mockito 测试 methodA(),以便我可以测试传递的垃圾数据或在我的测试用例中抛出异常.
I am writing Unit tests to test the methodA() using Mockito so that i can test passing junk data or throw exceptions in my testcases.
问题是我无法在 android 中模拟 AssetManager 类(它是最终的).
The problem is that i cannot mock AssetManager class in android (it being Final).
我尝试使用 InstrumentationTestCase 来注入真实和测试上下文,但这仅适用于少数场景.我如何控制 BufferedInputStream 以便我可以向它提供我想要的任何输入(使用模拟或其他方式)?
I tried to use InstrumentationTestCase to inject real and test context, but that only works for few scenarios. How do I control the BufferedInputStream so that I can provide it any input I want (using mocks or otherwise) ?
推荐答案
我不得不处理同样的问题.这就是我在不使用 Instrumentation 测试的情况下设法从我的单元测试程序访问配置文件的方式.(我使用 Android Studio 2.1).
I had to deal with the same problem. This is how I managed to access configuration file from my unit test programm without using Instrumentation test. (I use Android Studio 2.1).
单元测试代码:
public class ParametersTest {
Parameters parameters = null;
@Mock
Context context;
@Mock
AssetManager assetManager;
@Mock
InputStream inputStream;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);//create all @Mock objetcs
doReturn(assetManager).when(context).getAssets();
//parametersandroid.xml is located in test/resources directory
//parametersandroid.xml does not refer to a DTD file configuration.dtd
//get the full path name of the file
URL resource = ParametersTest.class.getClassLoader().getResource("parametersandroid.xml");
// to be used MyClass
// inside the method I want to be tested there is this statement :
// InputStream inputStream = this.assetManager.open(xmlFile);
InputStream inputStream=new FileInputStream(resource.getPath());
doReturn(inputStream).when(assetManager).open(anyString());
// AssetManager assetManager = context.getAssets();
// parameters = new Parameters(assetManager, resource.getPath());
parameters = new Parameters(context, resource.getPath());
}
@Test
public void testExtract() throws Exception {
assertEquals(parameters.extract("//database/index[@name='TeamNameIdx']/create").replaceAll("[^a-z,A-Z,;,.,?,']", ""),"createindexTeamNameIdxonTeamEntnameasc;");
}
}
待测试类的代码:
public class Parameters extends fr.acnice.valade.eric.gemara.utilities.Parameters {
private AssetManager assetManager = null;
public Parameters(Context context, String xmlFile) {
super(xmlFile);
this.assetManager = context.getAssets();
}
@Override
public String extract(String request) throws XPathExpressionException,
Exception {
InputStream inputStream = this.assetManager.open(super.xmlFile);
String result = (String) xPath.evaluate(request, new InputSource(inputStream),
XPathConstants.STRING);
if (result.isEmpty()) {
throw new Exception(
"Xpath result empty !!! check configuration file");
}
return result;
}
}
这篇关于模拟 Android AssetManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!