[英]UnitTest call /swagger-ui.html results in a 404 for springdoc-openapi
I am trying to migrate to springdoc-openapi;我正在尝试迁移到 springdoc-openapi; Everything runs great except being able to run unit-tests from mvn;除了能够从 mvn 运行单元测试之外,一切都运行良好; The following maven command results in a 404:以下 maven 命令会导致 404:
Intellij has no problem running it, so I suspect that it is a classloading issue. Intellij 运行它没有问题,所以我怀疑这是一个类加载问题。
I have used the code from the example below and just added my unit-test我使用了下面示例中的代码,并添加了我的单元测试
Guthub code: - https://github.com/eugenp/tutorials/tree/master/spring-boot-springdoc Guthub 代码: - https://github.com/eugenp/tutorials/tree/master/spring-boot-springdoc
For the unit-test I added to the pom.xml: (I use rest-assured to check if the swagger-ui page exists)对于我添加到 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>
The test itself:测试本身:
@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");
}
}
I found also someone that has the same problem: https://github.com/springdoc/springdoc-openapi/issues/99我也发现有人有同样的问题: https : //github.com/springdoc/springdoc-openapi/issues/99
Unfortunately no solution.不幸的是没有解决方案。 I could also go back to springfox.我也可以回到springfox。 Issues like this caused me to migrate: https://github.com/springfox/springfox/issues/2932 .像这样的问题导致我迁移: https : //github.com/springfox/springfox/issues/2932 。
You can have a look, at the sample you are mentioning from baeldung.您可以查看一下您从 baeldung 中提到的示例。 We have added it to the demos witht a sample test of the UI.我们已将它添加到演示中,并进行了 UI 示例测试。 (SwaggerUnitTest). (SwaggerUnitTest)。 Its passing on travis-CI without any issue.它通过 travis-CI 没有任何问题。
You can also have a look at the tests:您还可以查看测试:
将springdoc-openapi-ui
升级到 1.4.6 版后问题得到解决
maybe you need to add this lines into you WebSecurityConfigs
:也许您需要将此行添加到您的WebSecurityConfigs
:
.antMatchers("/v2/api-docs").permitAll()
.antMatchers("/swagger-ui.html").permitAll()
.antMatchers("/swagger-resources/**").permitAll()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.