繁体   English   中英

UnitTest 调用 /swagger-ui.html 导致 springdoc-openapi 404

[英]UnitTest call /swagger-ui.html results in a 404 for springdoc-openapi

我正在尝试迁移到 springdoc-openapi; 除了能够从 mvn 运行单元测试之外,一切都运行良好; 以下 maven 命令会导致 404:

  • mvn -Dtest=SwaggerUITest 测试 -f TestSwaggerUi -P 集成

Intellij 运行它没有问题,所以我怀疑这是一个类加载问题。

我使用了下面示例中的代码,并添加了我的单元测试

Guthub 代码: - https://github.com/eugenp/tutorials/tree/master/spring-boot-springdoc

对于我添加到 pom.xml 的单元测试:(我使用 rest-assured 来检查 swagger-ui 页面是否存在)

   <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
            <version>3.0.2</version>
    </dependency>

测试本身:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SwaggerUITest extends AbstractRestAssuredSetupClass {

    /**
     * Test successful swagger-UI call
     **/
    @Test
    public void getSwaggerUIPage() {
        givenAnAdministratorIsAuthenticated()
                .contentType(HTML)
                .get("/swagger-ui/index.html?url=/v3/api-docs")
                .then()
                .statusCode(OK.value());
    }
}

public abstract class AbstractRestAssuredSetupClass {

    @Value("${request.logging.enabled:true}")
    private boolean logRequests;
    @Value("${response.logging.enabled:true}")
    private boolean logResponses;
    @Value("${local.server.port}")
    private int port;

    @Before
    public void setUpRestAssured() {
        RestAssured.baseURI = "http://localhost:" + port;
        RestAssured.config = config().redirect(redirectConfig().followRedirects(false));
        if (logRequests) {
            RestAssured.filters(new RequestLoggingFilter());
        }
        if (logResponses) {
            RestAssured.filters(new ResponseLoggingFilter());
        }
    }

    protected RequestSpecification givenAnAdministratorIsAuthenticated() {
        return RestAssured.given().auth().preemptive().basic("user", "user");
    }
}

我也发现有人有同样的问题: https : //github.com/springdoc/springdoc-openapi/issues/99

不幸的是没有解决方案。 我也可以回到springfox。 像这样的问题导致我迁移: https : //github.com/springfox/springfox/issues/2932

您可以查看一下您从 baeldung 中提到的示例。 我们已将它添加到演示中,并进行了 UI 示例测试。 (SwaggerUnitTest)。 它通过 travis-CI 没有任何问题。

您还可以查看测试:

springdoc-openapi-ui升级到 1.4.6 版后问题得到解决

也许您需要将此行添加到您的WebSecurityConfigs

.antMatchers("/v2/api-docs").permitAll()
.antMatchers("/swagger-ui.html").permitAll()
.antMatchers("/swagger-resources/**").permitAll()

暂无
暂无

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

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