1.案例
使用的url如:
"http://localhost:8080/e/invoice/config/register/queryRegisterStatus?shopId=23591270"
2.比较
方法 | 优点 | 缺点 | 备注 |
---|---|---|---|
使用postman | 不用写代码 | 没法跟着项目代码走,容易丢失 |
|
使用springMockMVC | 像单测一样使用 | web项目每次都需要启动,启动耗时较长 |
|
使用HttpClient | 像单测一样使用 | web项目需要启动着 |
|
考虑可以结合使用,临时测试时使用postman,集成测试时使用httpClient。
3.使用
-
使用postman访问
-
使用springMockMvc
@Test
public void queryRegisterStatusTest1() throws Exception {
int shopId = 23591270;
InvoiceAccountBO accountBO = getAccount(shopId);
this.mockMvc
.perform(get("/e/invoice/config/register/queryRegisterStatus?shopId="+shopId)
.accept(MediaType.APPLICATION_JSON)
.requestAttr(Constant.INVOICE_ACCOUNT_BO,accountBO))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"));
System.out.println("end");
}
-
使用httpclient
@Test
public void queryRegisterStatusTest() {
int shopId = 23591270;
try {
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try {
HttpGet httpGet = new HttpGet(uri + "/e/invoice/config/register/queryRegisterStatus?shopId="+shopId);
client = HttpClients.createDefault();
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
String result = JSON.toJSONString(JSONObject.parseObject(EntityUtils.toString(entity)),true);
System.out.println("result:"+result);
} finally {
if (response != null) {
response.close();
}
if (client != null) {
client.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}