簡體   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