java - 如何测试多个服务调用

标签 java android testing junit android-espresso

我正在尝试增加我在 Android 上的代码覆盖率。但是我找不到正确的方法来测试这个演示者。 onSelectContact 进行服务调用,稍后我的 ServiceFactory.getContactService 进行另一个调用。我怎样才能模拟这个电话?

public class ContactListPresenter {

    public void onSelectContact(User user) {
        getCorrespondingContactCall(user).enqueue(new Callback <JsonElement>() {
            @Override
            public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
                switch (response.code()) {
                    case case1:
                        goToFirstActivity(user, response);
                        break;
                    case case2:
                        goToSecondActivity(user, response);
                        break;
                    default: showInvalidInput(user);
                        break;
                }
            }

            @Override
            public void onFailure(Call<JsonElement> call, Throwable throwable) {
                if (getView() != null) {
                    getView().showErrorView();
                }
            }
        });
    }

    protected Call<JsonElement> getCorrespondingContactCall(final User user) {
        return StringUtils.isValidEmail(user.getEmail())
                ? ServiceFactory.getContactService().checkContactByEmail(user.getEmail())
                : ServiceFactory.getContactService().checkContactByPhoneNumber(user.getPhoneNumber());
    }     

}

最佳答案

如果可以 - 消除静态调用。例如,通过显式注入(inject) ContactService到被测类:

public class ContactListPresenter {
    private final ContactService contactService;

    public ContactListPresenter(ContactService contactService) {
        this.contactService = contactService;
    }

    // rest of the code

    protected Call<JsonElement> getCorrespondingContactCall(final User user) {
        return StringUtils.isValidEmail(user.getEmail())
                ? contactService.checkContactByEmail(user.getEmail())
                : contactService.checkContactByPhoneNumber(user.getPhoneNumber());
    }     
}

这样,在测试中你就可以轻松模拟 Call<JsonElement>通过调用 contactService 返回.

但是,如果更改代码不是一种选择,您还有其他选择:

  • 使用 Powermock 模拟静态调用

  • 利用 getCorrespondingContactCall 的事实有protected通过在测试中创建匿名子类并 stub 调用 getCorrespondingContactCall 的结果来访问.例如:

public class ContactListPresenterTest {
   @Test
   public void test() {
      User user = ... // create user for test
      ContactListPresenter presenter = new ContactListPresenter() {
         @Override
         Call<JsonElement> getCorrespondingContactCall(final User user) {
            return ... // stub result of the call
         }
      };
      presenter.onSelectContact(user);
      // assert expected behavior
   }

}

关于java - 如何测试多个服务调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58143239/

相关文章:

Java XML 解析和写入导致缺少行尾

java - 如何创建动态二维数组并在其中存储打乱数组

java - Arrays.fill() 性能

java - 在 Selenium 中计时的最佳方式

java - 在 Selenium 中使用字符串外部测试

javascript - TestCafe - 断言不起作用(测试的对象必须是数组等)

Java JSch SSH 隧道发送 HTTP 或 HTTPs 请求到远程服务器

java - 具有所有负数的数组中的最大行总和

android - Xamarin Forms - 如何使 Entry 标签只读 (Android)

android - 遍历 Assets 文件夹 :( 时出现问题