Dave Weernink
Dave Weernink

Reputation: 91

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

I am trying to migrate to springdoc-openapi; Everything runs great except being able to run unit-tests from mvn; The following maven command results in a 404:

Intellij has no problem running it, so I suspect that it is a classloading issue.

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

For the unit-test I added to the pom.xml: (I use rest-assured to check if the swagger-ui page exists)

   <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

Unfortunately no solution. I could also go back to springfox. Issues like this caused me to migrate: https://github.com/springfox/springfox/issues/2932.

Upvotes: 0

Views: 2899

Answers (3)

Sobhan
Sobhan

Reputation: 1460

maybe you need to add this lines into you WebSecurityConfigs:

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

Upvotes: 0

probalm
probalm

Reputation: 61

The issue got fixed after upgrading springdoc-openapi-ui to version 1.4.6

Upvotes: 0

brianbro
brianbro

Reputation: 4769

You can have a look, at the sample you are mentioning from baeldung. We have added it to the demos witht a sample test of the UI. (SwaggerUnitTest). Its passing on travis-CI without any issue.

You can also have a look at the tests:

Upvotes: 1

Related Questions