我想测试存储库层,我正在使用spring webflux。我的测试课如下
@RunWith(SpringRunner.class)
@DataJpaTest
public class DataTester {
@Autowired
private MyRepository repository;
@Test
.....
}
即使在使用spring-weblux时在spring-mvc中也可以使用,我仍然收到以下错误消息。
Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.context.ApplicationContextException: Unable to start ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean.
该如何解决?如果我要使用@SpringBootApplication
启动整个应用程序上下文,那么它将起作用。还有其他没有使用的选项吗?
答案 0 :(得分:1)
这样做的原因是在application.properties
中,应用程序类型被设置为反应式。
spring.main.web-application-type=reactive
这将尝试自动配置Web服务器,在这种情况下为响应式Web服务器。由于@DataJpaTest
没有为此提供bean,因此失败了。可以通过两种方式解决此问题。
一种方法是在测试包的资源目录中添加一个application.properties
文件,并将值设置为sprig.main-web-application-type=none
,即可解决此问题。
或者我们可以简单地将属性值传递给注释,如下所示。 @DataJpaTest(properties = "spring.main.web-application-type=none")
答案 1 :(得分:0)
如果您正在使用Spring Boot 2+,那么在测试类上仅@DataJpaTest就足够了。 所以你的测试课应该是
@DataJpaTest
public class DataTester {
@Autowired
private MyRepository repository;
@Test
.....
}