-1

In my project I've implemented Spring 7 versioning.

But MockMvc tests and springdoc REST OpenApi documentation failed with exception MissingApiVersionException

1 Answer 1

-1
  • According REST API Versioning for MockMvc it is needed to configurate ApiVersionInserter and use .apiVersion() or just add .header() into MockHttpServletRequestBuilder builder

    @SpringBootTest
    @Transactional
    @AutoConfigureMockMvc
    @ActiveProfiles("test")
    //https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-testing-spring-boot-applications-testing-with-mock-environment
    public abstract class AbstractControllerTest {
    
        @Autowired
        private MockMvc mockMvc;
    
        protected ResultActions perform(MockHttpServletRequestBuilder builder) throws Exception {
            builder.header(WebConfig.VERSION_HEADER, WebConfig.CURRENT_VERSION);
            return mockMvc.perform(builder);
        }
    }
    

    Code: https://github.com/JavaOPs/bootjava/blob/boot4patched/src/test/java/ru/javaops/bootjava/AbstractControllerTest.java#L25

  • For springdoc it is needed add in version configuration

    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        public static final String VERSION_HEADER = "API-Version";
        public static final String CURRENT_VERSION = "1.0";
    
        @Override
        public void configureApiVersioning(ApiVersionConfigurer configurer) {
            configurer
                    .useRequestHeader(VERSION_HEADER)
                    .setVersionRequired(false)  <--- this or 
                    .setDefaultVersion(CURRENT_VERSION) <--- this or both
                    .addSupportedVersions(CURRENT_VERSION);
        }
    }
    

    Code: https://github.com/JavaOPs/bootjava/blob/boot4patched/src/main/java/ru/javaops/bootjava/app/config/WebConfig.java

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.