Android unit test version 2.0 using local JSON

I'm new to retrofit 2.0. I'd like to ask the best way to use this tool for unit testing, especially for asynchronous requests

I found a good article here. I am very interested in unit testing using local JSON static files because I think it will be faster and does not always require an Internet connection, but it will not work on retrofit 2.0 when I implement it. Can I do this in retrofit 2.0?

Maybe someone can give me a good reference here, or a good example of how to do these unit tests?

Sorry, my English is not good

This is a quick translation of the reference method of refurbishment 2 implemented by okhttp interceptor. I tested it quickly, but it's not too deep. Let me know if you have any problems

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();
    }
}

Add this interceptor to the custom okhttpclient-

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

Where context is an Android context

And add the customer to your transformation-

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>