【发布时间】:2020-01-04 07:58:45
【问题描述】:
我正在使用Web Flux 将一个项目从Play Framework 迁移到Spring Boot,并且当我手动启动它时它基本上可以运行。但是,我的大多数测试还没有工作。 (我的 play.Application 显然无法注入 org.springframework.beans.factory.BeanFactory,因为没有绑定任何实现。)使用 Play Framework,我曾经在我的测试中创建一个应用程序:
play.Application app = new play.inject.guice.GuiceApplicationBuilder()
.bindings(
// here I added a few manual bindings (e.g. mocks)
).build();
// Then I could get "beans" out of it:
MyFancyService fancyService = app.injector().instanceOf(MyFancyService.class);
// Or send Http-Requests to it:
Result response = route(app, request);
如何使用 Spring Boot 做到这一点?
我已经尝试/考虑过:
- 用
@SpringBootTest(SpringBootTest.WebEnvironment.MOCK)或@WebFluxTest注释我的所有测试,并添加@Autowired WebTestClient webClient。但是,尤其是在迁移期间,我宁愿不要过多地更改我的所有测试(上面的代码目前只在org.junit.Rule的一个地方)。 - 用
new SpringBootTestContextBootstrapper().buildTestContext().getApplicationContext()创建一个ApplicationContext,但不知道如何向它发送请求。
基本上我正在寻找 Spring Boot 中的这三个关键特性(如果可能的话,使用 Web Flux):
- 添加/注入一些模拟作为服务(手动添加绑定到 BeanFactory)
- 访问一些自动设置的服务(例如
app.getInstanceOf(MyFancyService.class)) - 发送 Http 请求
【问题讨论】:
-
第一个子弹是要走的路。
-
我知道这是个主意,但是我相信您可以以某种方式手动完成,我想知道如何做。 (而且我不想与框架如此紧密地耦合,尤其是在迁移期间,我宁愿不要过多地更改我的所有测试。)
-
使用框架的重点,就是使用框架。如果您不想太“束缚”,那么您不应该使用框架。然后,您应该自己启动服务并在构建过程中执行测试。部署并从 ansible 开始,使用 bash/python 脚本对服务执行带有 rest 调用的 jar。
标签: java spring spring-boot spring-webflux