Restmock服务器始终返回500

时间:2020-01-02 09:15:58

标签: android api retrofit retrofit2

MockWebServer [37929]收到请求:POST / users / sign_in HTTP / 1.1并响应:HTTP / 1.1 500服务器错误

我正在尝试使用下面的代码来模拟改造api的调用,但是从未调用过onResponse方法。

       public static Retrofit getClient() {

        RESTMockServerStarter.startSync(new JVMFileParser());
        RESTMockServer.whenPOST(pathContains("sign_in")).thenReturnFile(200, "login_v1.json");
        //Creating a retrofit object
        retrofit = new Retrofit.Builder()
                .baseUrl(RESTMockServer.getUrl())
                .addConverterFactory(GsonConverterFactory.create())
                .client(getOkHttpClient())
                .build();

        return retrofit;
    }

call.enqueue(new Callback<JsonElement>() {
                    @Override
                    public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
                        try {
                            if (response != null && response.isSuccessful()) {
                                if (response.body() != null) {
                                    JsonObject jsonResponse = response.body().getAsJsonObject();
                                    if (jsonResponse.get(ApiConstants.STATUS).isJsonPrimitive() && jsonResponse.get(ApiConstants.STATUS).getAsJsonPrimitive().isNumber()) {
                                        // Api call handling as per the status

                                    }
                                }
                            } else {
                                System.out.println("Fail---------------------------");

                            }
                        } catch (Exception e) {
                            System.out.println("Fail---------------------------");
                        }
                    }

                    @Override
                    public void onFailure(Call<JsonElement> call, Throwable t) {
                        //Show message to user for api failure
                        System.out.println("Fail---------------------------");
                    }

                });

添加了实现单元测试类。但是我总是收到MockWebServer [48913]收到的请求:POST / users / sign_in HTTP / 1.1并响应:HTTP / 1.1 500 Server Error。

package com.example;

import android.os.Build;

import com.example.webclient.ApiInterfaces;
import com.google.gson.JsonElement;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import io.appflate.restmock.JVMFileParser;
import io.appflate.restmock.RESTMockServer;
import io.appflate.restmock.RESTMockServerStarter;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static io.appflate.restmock.utils.RequestMatchers.pathEndsWith;

@RunWith(RobolectricTestRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class stubtest {

    private static final String PATH_ANDRZEJCHM_PROFILE = "src/test/resources/__files/city_v1.json";

    @Test
    @Config(sdk = Build.VERSION_CODES.O_MR1)
    public void exampleTest() {

        RESTMockServerStarter.startSync(new JVMFileParser());
        RESTMockServer.whenGET(pathEndsWith("/available_city_master")).thenReturnFile(PATH_ANDRZEJCHM_PROFILE);

        Retrofit retrofit;
        OkHttpClient client = new OkHttpClient();
        retrofit = new Retrofit.Builder()
                .baseUrl(RESTMockServer.getUrl())
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
        ApiInterfaces service = retrofit.create(ApiInterfaces.class);
        Call<JsonElement> call = service.getCity("surat", 1);

        call.enqueue(new Callback<JsonElement>() {
            @Override
            public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
                System.out.println("request" + response.body());
            }

            @Override
            public void onFailure(Call<JsonElement> call, Throwable t) {
                System.out.println("request" + t.getMessage());
            }
        });

    }
}

1 个答案:

答案 0 :(得分:0)

我认为您的做法不正确。从代码中还不清楚什么是RESTMockServerStarter。我建议您通过这些链接进行单元测试/模拟Web服务器

  1. How to unit test Retrofit api calls?
  2. https://medium.com/@hanru.yeh/unit-test-retrofit-and-mockwebserver-a3e4e81fd2a2