【发布时间】:2019-10-01 13:55:43
【问题描述】:
可以这么说,我正在调用第三方端点。我只是调用端点并解析响应。
public ListUsers listUsers() {
String url = "/api/v1/account/?apikey=" + apiKey;
String signature = null;
try {
signature = generateSignature(url);
} catch (SignatureException e) {
e.printStackTrace();
}
Call newCall = apiCall("GET", url, signature, null);
Type localVarReturnType = new TypeToken<ListUsers>() { }.getType();
ApiResponse<ListUsers> responseFromApiClient = apiClient.execute(newCall, localVarReturnType);
return responseFromApiClient.getData();
}
listUsers() 函数已被重构以获得更好的抽象
public ListUsers listUsers() {
String url = "/api/v1/account/?apikey=" + apiKey;
Type localVarReturnType = new TypeToken<ListUsers>() { }.getType();
ApiResponse<ListUsers> responseFromApiClient = apiClient.execute(buildApiCall("GET", url, null), localVarReturnType);
return responseFromApiClient.getData();
}
这个特定的端点不接受任何正文,并返回我然后解析的 json。
{
"meta": {
"limit": 0,
"offset": 0,
"total_count": 1
},
"objects": [
{
"address_one": "",
"address_three": "",
"address_two": "",
"country_code": "",
"cs_domain_id": null,
"date_joined": "2019-07-05T13:50:21",
"disable_personal_account": false,
"email": "noreply@some.com",
"first_name": "",
"id": 1,
"is_reseller": true,
"is_superuser": true,
"is_verified": true,
"last_login": null,
"last_name": "",
"paywall": true,
"postal_code": "",
"projects": [],
"reference_number": "",
"reseller_id": 1,
"reseller_name": "ROOT",
"reseller_rights": [
{
"domain_id": null,
"id": 1,
"level": "owner",
"name": "ROOT"
}
],
"resource_uri": "/api/v1/account/1/",
"saml": false,
"state": 0,
"status": "enabled",
"surname": "",
"username": "admin",
"uuid": "1c208540-3ed9-4741-a936-a574a3ded12a"
}
]
}
然后解析这个 json 响应。
我如何为此编写适当的单元测试,还是不需要它?我想出的唯一测试只是我实际调用服务的基本测试,但至于纯单元测试,它不适用于我的构建服务器。这是测试
@Test
void getListProjectsTest() {
GqConsoleApiClient gcac = new GqConsoleApiClient();
ListProjects response = gcac.listProjects();
System.out.println(listProjects);
Assert.assertEquals(20, response.getMeta().getLimit());
Assert.assertEquals("some-customer-id", response.getObjects().get(0).getCustomerId());
}
【问题讨论】:
标签: java rest unit-testing mocking mockito