Robolectric:在测试中模拟网络错误 [英] Robolectric: simulate network error in test

查看:100
本文介绍了Robolectric:在测试中模拟网络错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 robolectric 测试中产生与实际连接错误相同的异常?

How is it possible to produce the same exception like during a real connection-error in robolectric tests?

如果网络当前不可用,我想知道程序如何运行.是否有可能为我的 HttpClient 产生相同的异常?

I want to how the program acts if the network is currently not available. Is there a possibility to produce the same exception for my HttpClient?

我已经试过了:

Robolectric.getFakeHttpLayer().interceptHttpRequests(false); // with real network to a non existent IP

WifiManager wifiManager = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);

Robolectric.addPendingHttpResponse(404, null);

但它们都不会产生像真正的断开连接那样的反应.

but none of them produces the same reactions like a real connection-loosing.

谢谢

推荐答案

我已经检查了 RobolectricFakeHttpLayer 并没有找到模拟抛出 <代码>IOException.

I've checked Robolectric's FakeHttpLayer and haven't found way to simulate throwing an IOException.

所以使用模拟来让它为你工作.首先引入HttpClientFactory(如果你使用HttpClient,你可以对HttpUrlConnection使用同样的方法):

So use mocking to make it working for you. First introduce HttpClientFactory (if you use HttpClient, you can use same approach for HttpUrlConnection):

public class HttpClientFactory {
    public HttpClient createClient() {
     return new DefaultHttpClient();
    }
}

现在在您的网络层中使用工厂而不是构造函数(为了简单起见,假设它是同步的):

And now in your networking layer use factory instead of constructors (let for simplicity assume that it is synchronous):

public class HttpTransportLayer {
    private final HttpClientFactory clientFactory;

    public HttpTransportLayer() {
        this(new HttpClientFactory());
    }

    // For tests only
    HttpTransportLayer(HttpClientFactory clientFactory) {
        this.clientFactory = clientFactory;
    }

    public String requestData(String url) {
        HttpClient client = factory.createClient();
        ...
    } 
}

所以现在你可以在测试中使用 Mockito:

So now you can in tests use Mockito:

HttpClient mockedClient = mock(HttpClient.class);

@Before
public void setUp() {
     HttpClientFactory factory = mock(HttpClientFactory.class);
     when(factory.createClient()).thenReturn(mockedClient);

     target = new HttpTransportLayer(factory);
}

@Test
public void whenIOExceptionThenReturnNull() {
    when(mockedClient.execute(any(HtptUriRequest.class))).thenThrow(new IOException());

    String data = target.requestData("http://google.com");

    assertThat(data).isNull();
}

这是虚拟测试,通常没有人会在出错时返回 null.

That is dummy test and usually nobody will return null in case of error.

您还可以使用诸如 Dagger 之类的依赖注入框架来最小化注入代码.

You could also task look to some dependency injection framework like Dagger to minimise injection code.

如果你使用任何好的网络框架,比如 RetrofitVolley 那么它就更简单了 - 你不需要模拟任何东西,只需调用你的错误回调.

If you use any good framework for networking like Retrofit or Volley then it is even simpler - you don't need to mock anything and just invoke you error callback.

希望能帮到你

这篇关于Robolectric:在测试中模拟网络错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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