我有一个定制的spring-boot-starter,当它收到的spring应用程序事件时将调用一些REST API ApplicationReadyEvent
,因此配置类类似于:
@Configuration
public class MySpringBootStarter {
@EventListener(ApplicationReadyEvent.class)
public void init() {
// Call REST APIs here
}
}
然后,我想测试启动器,MockServer
它需要在运行测试之前创建一些期望值。测试类可能如下所示:
@ExtendWith(MockServerExtension.class)
@SpringBootTest
@ContextConfiguration
@MockServerSettings(ports = {28787, 28888})
public class MySpringBootStarterTest {
private MockServerClient client;
@BeforeEach
public void beforeEachLifecycleMethod(MockServerClient client) {
this.client = client;
//creating expectations here
}
@Test
void shouldBeTrue() {
assertThat(true).isTrue();
}
@SpringBootApplication
static class MyTest {
public void main(String[] args) {
SpringApplication.run(Test.class, args);
}
}
}
但实际上,在后总是产生的期望ApplicationReadyEvent
,即,该init
方法MySpringBootStarter
类之前的所谓beforeEachLifecycleMethod
的方法MySpringBootStarterTest
类。
请问如何进行测试?
您可以static
在SpringContext启动之前使用块初始化程序运行所需的代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句