小编典典

在Spring Boot 1.4中使用@DataJpaTest和SpringFox @ EnableSwagger2进行切片测试

spring-boot

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

我尝试了@DataJpaTest来测试我的存储库,但是我的应用程序正在使用Springfox,因此使用Springfox @
EnableSwagger2时,测试执行将失败,并出现以下错误:

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

该如何解决?否则,将无法使用@DataJpaTest进行切片测试。

码:

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("/")
        ;
  }
}

测试类别:

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

  @Test
  public void doNothing() {
  }
}

阅读 383

收藏
2020-05-30

共1个答案

小编典典

将@EnableSwagger从SpringBootApplication中移出

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

@Configuration
@EnableSwagger2
class AdditionalConfig {

}
2020-05-30