如何在 Spring Boot 1.4 中使用 @DataJpaTest 和 SpringFox @EnableSwagger2 进行切片测试 [英] How do do slice testing in Spring Boot 1.4 using @DataJpaTest with SpringFox @EnableSwagger2

查看:32
本文介绍了如何在 Spring Boot 1.4 中使用 @DataJpaTest 和 SpringFox @EnableSwagger2 进行切片测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

可以做些什么来解决这个问题?否则,无法使用@DataJpaTest 进行切片测试.

What can be done to address this? Otherwise, it is impossible to do slice testing using @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() {
  }
}

推荐答案

将 @EnableSwagger 移出 SpringBootApplication

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 {

}

这篇关于如何在 Spring Boot 1.4 中使用 @DataJpaTest 和 SpringFox @EnableSwagger2 进行切片测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆