给定一个Spring Boot应用程序,它在启动时执行一些需要模拟服务器的操作:
@SpringBootApplication
public class Application implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
System.err.println("should be 2nd: application init (needs the mock server)");
}
public boolean doSomething() {
System.err.println("should be 3rd: test execution");
return true;
}
}
因此,应提前初始化模拟服务器:
@SpringBootTest
@TestInstance(Lifecycle.PER_CLASS)
class ApplicationITest {
@Autowired
MockServer mockServer;
@Autowired
Application underTest;
@BeforeAll
void setUpInfrastructure() {
mockServer.init();
}
@Test
void doSomething() {
assertTrue(underTest.doSomething());
}
}
@Component
class MockServer {
void init() {
System.err.println("should be 1st: mock server init");
}
}
但应用程序似乎总是先初始化:
should be 2nd: application init (needs the mock server)
should be 1st: mock server init
should be 3rd: test execution
我怎样才能按预定的顺序执行这些任务?
我试着用
Order(Ordered.HIGHEST_PRECEDENCE)
和
@AutoConfigureBefore
没有成功。