1 回答
TA贡献1847条经验 获得超7个赞
根据Spencer的建议,我使用弹簧应用程序构建器创建了一个完整的Web应用程序。我也在自动配置模块之外执行此操作 - 在类路径上创建了一个名为“集成测试”的新 maven 子模块。spring-boot-starter-web
@Import(MyAutoConfig.class)
@SpringBootApplication
public class MinStarterBasicApp {
@Bean
ServiceRegistry serviceRegistry() {
return mock(ServiceRegistry.class);
}
static ConfigurableApplicationContext setupWebApp(String... profiles){
System.setProperty("spring.main.allow-bean-definition-overriding", "true");
SpringApplication app = new SpringApplicationBuilder(MinStarterBasicApp.class)
.web(WebApplicationType.SERVLET)
.profiles(profiles)
.build();
return app.run();
}
}
其中允许我按名称传入应用程序.properties文件,如下所示。此外,请务必确保为每个测试手动提供应用上下文。profilesclose
public class StarterBasicAutoconfigTest {
ConfigurableApplicationContext context;
@After
public void teardown() {
if (context != null && context.isRunning())
context.close();
}
@Test
public void sometest() {
context = MinStarterBasicApp.setupWebApp("profile1");
ServiceRegistry registry = context.getBean(ServiceRegistry.class);
context.close();
Mockito.verify(registry, times(1)).register(any());
Mockito.verify(registry, times(1)).deregister(any());
}
添加回答
举报