Spring 如何在测试中的ApplicationEvent侦听器之前进行一些设置
我有一个定制的spring启动程序,当它获得ApplicationReadyEvent的spring应用程序事件时,它将调用一些REST API,因此配置类类似于:Spring 如何在测试中的ApplicationEvent侦听器之前进行一些设置,spring,spring-boot,spring-test,spring-boot-test,mockserver,Spring,Spring Boot,Spring Test,Spring Boot Test,Mockserver,我有一个定制的spring启动程序,当它获得ApplicationReadyEvent的spring应用程序事件时,它将调用一些REST API,因此配置类类似于: @Configuration public class MySpringBootStarter { @EventListener(ApplicationReadyEvent.class) public void init() { // Call REST APIs here } } 然后,
@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之后创建的,也就是说,MySpringBootStarter类的init方法在MySpringBootStarterTest类中的BeforeAchLifeCycleMethod方法之前被调用
请告诉我如何使测试工作?您可以在SpringContext启动之前使用静态块初始值设定项运行所需的代码。您可以在SpringContext启动之前使用静态块初始值设定项运行所需的代码