How do do slice testing in Spring Boot 1.4 using @DataJpaTest with SpringFox @EnableSwagger2

不问归期 提交于 2019-12-10 15:06:10

问题


Re: https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4

I tried the @DataJpaTest to test my repository but my application is using Springfox, so with Springfox @EnableSwagger2 the test execution will fail with the following error:

java.lang.IllegalStateException: Failed to load ApplicationContext
...    
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined
...
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.List<org.springframework.web.servlet.mvc.method.requestmappinginfohandlermapping>' available

What can be done to address this? Otherwise, it is impossible to do slice testing using @DataJpaTest.

Code:

Application class:
@SpringBootApplication
@EnableSwagger2
public class CurrencyApplication {
  @Bean
  public Module datatypeHibernateModule() {
    return new Hibernate5Module();
  }

  public static void main(String[] args) {
    SpringApplication.run(CurrencyApplication.class, args);
  }

  @Bean
  public Docket currencyApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.any())
        .build()
        .pathMapping("/")
        ;
  }
}

Test class:

@RunWith(SpringRunner.class)
@DataJpaTest
public class ExchangeRateRepoTest {

  @Test
  public void doNothing() {
  }
}

回答1:


Move @EnableSwagger out of the SpringBootApplication

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@Configuration
@EnableSwagger2
class AdditionalConfig {

}


来源:https://stackoverflow.com/questions/41673151/how-do-do-slice-testing-in-spring-boot-1-4-using-datajpatest-with-springfox-en

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!