gpt4 book ai didi

安卓测试。模拟 HttpUrlConnection(部分)

转载 作者:可可西里 更新时间:2023-11-01 17:29:23 25 4
gpt4 key购买 nike

我必须创建一个测试系统来拦截所有 HTTP 请求,但应该只处理少数请求,经过一些研究我发现 this展示如何模拟 HttpUrlConnection 的文章,但我必须适应,我只想将其用于测试中的请求,其他请求应像往常一样通过互联网,我的尝试是返回不同的 HttpUrlConnection 对象,这些对象取决于 url 和显然没有用。

    public class MockURLStreamHandler extends URLStreamHandler implements URLStreamHandlerFactory
{
..............................
// *** URLStreamHandler

@Override
protected URLConnection openConnection(URL u) throws IOException {

mConnection = new MockHttpURLConnection(u);
if(u.toString().equals(myTestUrl))
{
return mConnection;
}
else
{
//this doesn't work
return new HttpUrlConnection(u){..}
}
}
...............................
}

有什么想法吗?在我应该寻找什么之后?

提前致谢。

最佳答案

看看MockWebServer

This library makes it easy to test that your app Does The Right Thing when it makes HTTP and HTTPS calls. It lets you specify which responses to return and then verify that requests were made as expected.

Because it exercises your full HTTP stack, you can be confident that you're testing everything. You can even copy & paste HTTP responses from your real web server to create representative test cases. Or test that your code survives in awkward-to-reproduce situations like 500 errors or slow-loading responses.

关于安卓测试。模拟 HttpUrlConnection(部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40003803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com