模拟Android AssetManager

Aks*_*hat 5 android unit-testing mockito

我有一段代码接受Context并将此上下文传递给私有方法.private方法调用getAssets().open()来读取我的app的assets文件夹中的文件.

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[];
}
Run Code Online (Sandbox Code Playgroud)

我正在编写单元测试以使用Mockito测试方法A(),以便我可以测试传递垃圾数据或在我的测试用例中抛出异常.

问题是我无法在android中模拟AssetManager类(它是Final).

我尝试使用InstrumentationTestCase来注入实际和测试上下文,但这仅适用于少数场景.如何控制BufferedInputStream以便我可以提供任何我想要的输入(使用模拟或其他方式)?

小智 6

我不得不处理同样的问题.这就是我在不使用Instrumentation测试的情况下从单元测试程序中访问配置文件的方法.(我使用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;");
    }
}
Run Code Online (Sandbox Code Playgroud)

要测试的类的代码:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)