Spring boot 带反作用弹簧的数据JPA测试

Spring boot 带反作用弹簧的数据JPA测试,spring-boot,jpa,spring-webflux,Spring Boot,Jpa,Spring Webflux,我想测试存储库层,我正在使用SpringWebFlux。我的测试课程如下 @RunWith(SpringRunner.class) @DataJpaTest public class DataTester { @Autowired private MyRepository repository; @Test ..... } 尽管在使用SpringWebLux时,这在SpringMVC中是可行的,但我得到了以下错误 Failed to load Applic

我想测试存储库层,我正在使用SpringWebFlux。我的测试课程如下

@RunWith(SpringRunner.class)
@DataJpaTest
public class DataTester {
    @Autowired
    private MyRepository repository;

    @Test
    .....

}

尽管在使用SpringWebLux时,这在SpringMVC中是可行的,但我得到了以下错误

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
启动整个应用程序上下文,它会工作。没有使用该选项的任何其他选项?

如果您使用的是Spring Boot 2+,那么在测试类上只有@DataJpaTest就足够了。 所以你的测试类应该是

@DataJpaTest
public class DataTester {
    @Autowired
    private MyRepository repository;

    @Test
    .....

}

若您使用的是SpringBoot2+,那个么在测试类上只有@DataJpaTest就足够了。 所以你的测试类应该是

@DataJpaTest
public class DataTester {
    @Autowired
    private MyRepository repository;

    @Test
    .....

}

原因是在
application.properties
中,应用程序类型设置为reactive

spring.main.web应用程序类型=reactive

这将尝试自动配置web服务器,在本例中为反应式web服务器。由于
@DataJpaTest
没有为此提供bean,因此失败。这可以通过两种方式修复

一种方法是在测试包的resources目录中添加一个
application.properties
文件,并将该值设置为,
sprig.main web application type=none
解决了这个问题


或者我们可以简单地将属性值传递给注释,如下所示
@DataJpaTest(properties=“spring.main.web应用程序类型=none”)

原因是在
应用程序.properties中,应用程序类型设置为被动

spring.main.web应用程序类型=reactive

这将尝试自动配置web服务器,在本例中为反应式web服务器。由于
@DataJpaTest
没有为此提供bean,因此失败。这可以通过两种方式修复

一种方法是在测试包的resources目录中添加一个
application.properties
文件,并将该值设置为,
sprig.main web application type=none
解决了这个问题


或者我们可以简单地将属性值传递给注释,如下所示
@DataJpaTest(properties=“spring.main.web application type=none”)

这会产生一个空指针异常,因为当应用程序@Yomal中出现其他问题时,存储库不是自动连接的,因为文档明确说明您只需要
@DataJpaTest
我正在使用junit 4。排除
@Runwith
@Extendwith
只能在Junit5@Yomal您使用的是哪个spring boot版本?Springboot版本2.2.5这会产生一个空指针异常,因为存储库不是自动连接的,当您的应用程序@Yomal中出现其他问题时,因为文档明确指出您只需要
@DataJpaTest
我正在使用JUnit4。排除
@Runwith
@Extendwith
只能在Junit5@Yomal您使用的是哪个Springboot版本?Springboot版本2.2.5