gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:52 24 4
gpt4 key购买 nike

回复: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() {
}
}

最佳答案

将@EnableSwagger移出SpringBootApplication

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

@Configuration
@EnableSwagger2
class AdditionalConfig {

}

关于java - 如何使用 @DataJpaTest 和 SpringFox @EnableSwagger2 在 Spring Boot 1.4 中进行切片测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41673151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com