【问题标题】:Android: Generate a Mock Intent and return some intent-data from itAndroid:生成一个模拟意图并从中返回一些意图数据
【发布时间】:2013-11-27 13:30:25
【问题描述】:

我正在使用ActivityInstrumentationTestCase2 对我的应用程序进行单元测试。我有一个案例,用户单击活动“A”的按钮,然后打开一个新的活动“B”,用户将在其中做一些工作,最后一些数据通过 setResult(RESULT_OK,intent) 返回到活动“A” ;

现在,我想通过 jUnit 进行测试,并想模拟这个工作流,这样就不需要用户交互。有人可以帮我解决这个问题吗?

PS:我不想为此使用 Robotium..我只想通过普通的 ActivityInstrumentationTestCase2 和一些 Mocking 框架来实现。

【问题讨论】:

    标签: android junit mockito easymock


    【解决方案1】:

    试试这个代码

    // Check the intent which was started
        Intent triggeredIntent = getStartedActivityIntent();
        assertNotNull("Intent was null", triggeredIntent);
        String data = triggeredIntent.getExtras().getString("URL");
    
    //And
    
    @Override 
    protected void setUp() throws Exception {
        super.setUp();
        Intent intent = new Intent(getInstrumentation().getTargetContext(),
            MainActivity.class);
        startActivity(intent, null, null);
        activity = getActivity();
      }
    

    详情:Follow this link

    【讨论】:

    • 此代码实际上在 ActivityInstrumentationTestCase2 中不起作用 - 您需要 ActivityUnitTestCase 才能使用 getStartedActivityIntent() 方法。
    猜你喜欢
    • 2012-08-19
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    相关资源
    最近更新 更多