Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

java - UnitTest调用/swagger-ui.html导致springdoc-openapi的404(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:)

  • mvn -Dtest=SwaggerUITest test -f TestSwaggerUi -P integration

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

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

  ask by Dave Weernink translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

(您还可以查看测试:)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...