Android的测试项目 - 阅读资源文件来测试普通Java对象 [英] Android Test Project - Reading assets file to test plain java object

查看:148
本文介绍了Android的测试项目 - 阅读资源文件来测试普通Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目设置

  • 图书馆项目A 的包含一个fooJSONParser是这样的:

  • Library Project A Contains a fooJSONParser like this :

public class fooJsonParser {
  ...
  // Constructor using the input-stream
  public DPNodeJsonParser(InputStream inputJsonStream) {
    // Constructor
  }

  /**
   * Method that has to be executed once the class is instantiated to parse
   */
  public parsedObject parseObj() {
    // Parses and prints the info
  }

  • 应用项目B 的,它使用的图书馆项目A

  • App Project B, which uses the Library Project A

    问题

    现在我已经创建了一个测试项目的测试的测试的应用项目B 的并添加一个测试用例来测试 fooJsonParser 类。每当有新的JSON数据,我更新了解析方法,并希望使用的测试的项目来测试一下,看看是否解析器解析正确,而不是安装,运行应用程序并导航到屏幕,即新JSON数据将被使用。为了实现这一点,我有在资产JSON文件的测试文件夹。但我不知道我怎么可以读取JSON文件。我觉得这是与 MockContext ,但不知道如何使用它作为的文档说,所有的方法都不起作用。

    Now I have created a Test Project tester to test the App Project B and add a test case to test the fooJsonParser class. Everytime there is new json data, I update the parse method and want to test it using the tester project and see if the parser is parsing correctly instead of installing, running the app and navigating to the screen where that new json data would be used. For accomplishing this, I am having a json file in the assets folder of the tester. But I'm not sure how I can read the json file. I think this has something to do with MockContext but wasn't sure how to use it as the documentation says all methods are non-functional.

    可能有人提出一个解决方案/提供样本从的普通Java对象的测试用例

    Could somebody suggest a solution/provide a sample to read the asset file from a plain java object test case?

    我是相当新的Andr​​oid的测试环境。让我知道,如果有什么不清楚。我可以添加更多的信息。

    I'm fairly new to the android testing environment. Let me know If anything is unclear. I can add more information.

    推荐答案

    我这解决了以下内容:

    • 创建的安装作为解释这里
    • 扩展的 InstrumentationTestCase 而不是的TestCase 。该 InstrumentationTestCase 的方法 getInstrument(),通过它我获得了仪表的(测试应用程序项目)上下文。
    • Created the setup as explained here
    • Extended the InstrumentationTestCase instead of TestCase. The InstrumentationTestCase has the method getInstrument() through which I got access to the Instrumentation's(The test app project) Context.

    这篇关于Android的测试项目 - 阅读资源文件来测试普通Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆