Java:401 spring引导的单元测试用例中存在未经授权的测试用例问题,REST
各位 我已经编写了一个测试用例: AppManagementApplicationTests.javaJava:401 spring引导的单元测试用例中存在未经授权的测试用例问题,REST,java,spring-boot,junit,http-status-code-401,testcase,Java,Spring Boot,Junit,Http Status Code 401,Testcase,各位 我已经编写了一个测试用例: AppManagementApplicationTests.java @Test public void testWithAppNameAsNull() { Input input = new Input(); // input type String inputTrue = input.appNameAsNullOfAppInfo(); // response has been stored Response resp
@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是,在错误中显示
如果不是401unauthorized,而是将其更改为400bad_请求,那么相同的测试用例绝对可以正常工作。
我不知道为什么。显示错误的行号139是“ResponseEntity result=“
提前感谢。url:是由服务器(Tomcat)定义的应用程序url
url:是嵌入式服务器为您的测试用例生成的url。。它由配置webEnvironment=SpringBootTest.webEnvironment.RANDOM\u端口创建
查看您的测试,它看起来不像是身份验证问题,但似乎传递给您的服务的输入参数不正确。url:是由您的服务器(Tomcat)定义的应用程序url
url:是嵌入式服务器为您的测试用例生成的url。。它由配置webEnvironment=SpringBootTest.webEnvironment.RANDOM\u端口创建
查看您的测试,它看起来不像是身份验证问题,但似乎传递给服务的输入参数不正确。这是一个非常简单的解决方案,只需在我的“pom.xml”文件中添加一个依赖项:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
org.apache.httpcomponents
httpclient
测试
现在,我的测试用例运行得非常好。这是一个非常简单的解决方案,只需在我的“pom.xml”文件中添加一个依赖项:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
org.apache.httpcomponents
httpclient
测试
现在我的测试用例运行得非常好。谢谢,@Teena。我传递的输入参数是正确的,缺少的是我的“pom”文件中的依赖项。经过大量的研究,我发现了一个,现在我的测试用例工作得非常好。再次感谢你的帮助。谢谢,@Teena。我传递的输入参数是正确的,缺少的是我的“pom”文件中的依赖项。经过大量的研究,我发现了一个,现在我的测试用例工作得非常好。再次感谢你的帮助。