我是单元测试的新手。我的要求是检查响应的格式是否正确。我正在发布已完成的代码。这是实现Mockito的正确方法。在编译代码时,会抛出错误,说实际上,与此模拟程序之间的交互为零。你能给一些想法如何模拟测试响应格式吗?
public class SignUpTest {
MockWebServer mockWebServer;
@Mock
ApiInterface apiInterface;
@Mock
Call<ResponseBody> responseBodyCall;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testWithCorrect() throws Exception {
String format = "{" +
"\"Status\":null," +
"\"Message\":null," +
"\"IssueId\":null," +
"\"TransId\":null," +
"\"ResponseCode\":101," +
"\"ResponseDescription\":\"APP requires your phone id while registering app." +
" Please provide necessary permission to the app before proceeding.\"" +
"}";
JsonObject jsonParam = new JsonObject();
jsonParam.addProperty("msisdn", "9812345678");
when(apiInterface.appLogin(jsonParam)).thenReturn(responseBodyCall);
verify(responseBodyCall).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
assert response.body() != null;
assertEquals(format, response.body().toString());
}
@Override
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
}
});
}