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());
}
添加回答
舉報