我有一个自定义的 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
请问我怎样才能使测试工作?