【问题标题】:Retrofit 2.0 Android Unit Testing using Local JSONRetrofit 2.0 使用本地 JSON 的 Android 单元测试
【发布时间】:2020-01-17 07:33:58
【问题描述】:

我是 Retrofit 2.0 的新手,我想询问使用它进行单元测试的最佳方法,尤其是对于异步请求。

我找到了一篇关于它的好文章 here,我对使用本地 JSON 静态文件进行单元测试很感兴趣,因为我认为它会更快,并且并不总是需要互联网连接,但它不会当我在 Retrofit 2.0 上实现它时不起作用。是否可以在 Retrofit 2.0 中做到这一点?

或者也许有人可以在这里帮助我提供好的参考资料或者一些关于如何进行这些单元测试的好例子?

对不起,我的英语不好。

【问题讨论】:

    标签: android json unit-testing retrofit


    【解决方案1】:

    这里是使用 OkHttp Interceptor 实现的改造 2 的参考方法的快速翻译。我给了它一个快速测试,但没有太深。如果您有任何问题,请告诉我。

    public class LocalResponseInterceptor implements Interceptor {
    
        private Context context;
    
        private String scenario = null;
    
        public LocalResponseInterceptor(Context ctx) {
            this.context = ctx;
        }
    
        public void setScenario(String scenario) {
            this.scenario = scenario;
        }
    
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            URL requestedUrl = request.url();
            String requestedMethod = request.method();
    
            String prefix = "";
            if (this.scenario != null) {
                prefix = scenario + "_";
            }
    
            String fileName = (prefix + requestedMethod + requestedUrl.getPath()).replace("/", "_");
            fileName = fileName.toLowerCase();
    
            int resourceId = context.getResources().getIdentifier(fileName, "raw",
                    context.getPackageName());
    
            if (resourceId == 0) {
                Log.wtf("YourTag", "Could not find res/raw/" + fileName + ".json");
                throw new IOException("Could not find res/raw/" + fileName + ".json");
            }
    
            InputStream inputStream = context.getResources().openRawResource(resourceId);
    
            String mimeType = URLConnection.guessContentTypeFromStream(inputStream);
            if (mimeType == null) {
                mimeType = "application/json";
            }
    
            Buffer input = new Buffer().readFrom(inputStream);
    
            return new Response.Builder()
                    .request(request)
                    .protocol(Protocol.HTTP_1_1)
                    .code(200)
                    .body(ResponseBody.create(MediaType.parse(mimeType), input.size(), input))
                    .build();
        }
    }
    

    将此拦截器添加到自定义OkHttpClient --

        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.interceptors().add(new LocalResponseInterceptor(context));
    

    其中context 是一个安卓Context

    并将该客户端添加到您的改造中 --

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.github.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build();
    

    【讨论】:

    • 我可以看看你如何在单元测试中使用它吗?我尝试使用 Robolectric 使用它,但出现此错误 Exception in thread "OkHttp Dispatcher" java.lang.NullPointerException
    • @Michael 你明白了吗?我知道这是很久以前的了..
    猜你喜欢
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 2017-07-12
    • 2016-10-03
    相关资源
    最近更新 更多