每个人。
我写了一个测试用例:
AppManagementApplicationTests.java
@Test
public void testWithAppNameAsNull() {
Input input = new Input();
// input type
String inputTrue = input.appNameAsNullOfAppInfo();
// response has been stored
Response response = Response.status(401).entity("").build();
HttpEntity<String> passingData = new HttpEntity<String>(inputTrue);
ResponseEntity<String> result = this.restTemplate.postForEntity("/App", passingData, String.class);
assertEquals(response.getStatus(), result.getStatusCodeValue());
}
输入法
public String appNameAsNullOfAppInfo() {
String appName = null;
String appVersion = "1.1";
String appKey = "testkey";
long timestamp = 1487076718;
AppInfo obj = new AppInfo(appName, appVersion, appKey, timestamp);
Gson gson = new Gson();
// searialize into string and return
return gson.toJson(obj);
}
在我的代码中,我正在发送
返回新的 ResponseEntity(gson.toJson(response), HttpStatus.UNAUTHORIZED);
因此,当我使用 Postman 运行它时,它的状态代码为 401
我使用的 URL 是http://localhost:8091/App/并且在错误中显示http://localhost:41636/App。
如果不是 401 UNAUTHORIZED 我将其更改为 400 BAD_REQUEST,则相同的测试用例绝对可以正常工作。我不知道为什么。显示错误的第 139 行是“ResponseEntity result ="
提前致谢。
一个很简单的解决方案,只需要在我的“pom.xml”文件中添加一个依赖项:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
现在我的测试用例工作得很好。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句