Robolectric + OkHttp +改造+ rxJava单元测试

 劲舞团之舞之精灵_559 发布于 2022-12-25 12:32

我正在尝试用robolectric编写一段代码的单元测试.问题是我需要伪造http调用,但似乎robolectric的假层只适用于Apache的HttpClient,根据这个答案:

链接回答

在Retrofit中,您无法更改URL,因此MockWebServer似乎不是一个选项.

似乎mockito可以捕获改进的回调,但我正在使用rxJava,所以我真的不知道它是否有帮助.

有没有人对Robolectric + Retrofit + okHttp + rxJava的单元测试有任何建议?

这是一小段代码:

    @Test
public void test1() throws IOException, InterruptedException {
    FragmentA frag = (FragmentA) activity
            .getFragmentManager().findFragmentById(
                    R.id.frag);
    assertThat(frag).isNotNull();
    assertThat(frag.isVisible()).isTrue();

    EditText input1 = (EditText) frag.getView()
            .findViewById(R.id.edit_text1);
    EditText input2 = (EditText) frag.getView()
            .findViewById(R.id.edit_text2);
    Button button = (button) frag.getView()
            .findViewById(R.id.button);
    input1.setText("999");
    input2.setText("999");
    Robolectric.addPendingHttpResponse(200, "{\"isValid\": true}");
    button.performClick();
            assertThat(
            ShadowAlertDialog.getLatestDialog() instanceof ProgressDialog)
            .isTrue();

  }

由于OkHttp,Robolectric.addPendingHttpResponse无论如何都不会起作用.按下按钮时启动api调用,因此我需要伪造响应!

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有