Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何在测试中的ApplicationEvent侦听器之前进行一些设置_Spring_Spring Boot_Spring Test_Spring Boot Test_Mockserver - Fatal编程技术网

Spring 如何在测试中的ApplicationEvent侦听器之前进行一些设置

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 } } 然后,

我有一个定制的spring启动程序,当它获得ApplicationReadyEvent的spring应用程序事件时,它将调用一些REST API,因此配置类类似于:

@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启动之前使用静态块初始值设定项运行所需的代码