简体   繁体   English

Java:用于弹簧启动,REST的单元测试用例中的401未经授权的测试用例问题

[英]Java: 401 Unauthorized test case issue in Unit test case for spring boot, REST

everyone. 大家。

I have written a test case: 我写了一个测试用例:

AppManagementApplicationTests.java 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());

}

Input.java Input.java

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);

}

In my code I am sending 在我的代码中,我正在发送

return new ResponseEntity(gson.toJson(response), HttpStatus.UNAUTHORIZED); 返回新的ResponseEntity(gson.toJson(response),HttpStatus.UNAUTHORIZED);

So when I run it using Postman it gives the status code as 401 因此,当我使用Postman运行它时,其状态代码为401

在此处输入图片说明

But while testing it is giving an error 但是在测试时却给出了错误 在此处输入图片说明

The URL that I am using is http://localhost:8091/App/ and in the error it is showing http://localhost:41636/App . 我使用的URL是http:// localhost:8091 / App /,并且在错误中显示http:// localhost:41636 / App

And if instead of 401 UNAUTHORISED I change it to 400 BAD_REQUEST, the same test case works absolutely fine. 如果我将其更改为400 BAD_REQUEST,而不是401 UNAUTHORIZED,则相同的测试用例绝对可以正常工作。 I am not able to figure out why. 我不知道为什么。 The line number 139 at which it is showing the error is "ResponseEntity result =" 显示错误所在的行号139是“ ResponseEntity result =“

Thanks in advance. 提前致谢。

The url : http://localhost:8091/App/ is your app url defined by your server (Tomcat). URL: http:// localhost:8091 / App /是您的服务器(Tomcat)定义的应用程序URL。

The url : http://localhost:41636/App is the one generated by the embedded server for your test case.. It gets created by the config webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT url: http:// localhost:41636 / App是嵌入式服务器为您的测试用例生成的URL。它由配置webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT创建。

Looking at your test, it doesn't look like an authentication issue, but it seems that the inputs parameters passed to your service are incorrect.. 查看您的测试,它看起来不像是身份验证问题,但似乎传递给您服务的输入参数不正确。

Quite a simple solution, just need to add a dependency in my 'pom.xml' file: 一个非常简单的解决方案,只需要在我的“ pom.xml”文件中添加一个依赖项即可:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <scope>test</scope>
</dependency>

Now my test case is working absolutely fine. 现在我的测试用例完全正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM