如何在应用程序运行之前执行 SpringBootTest 中的代码?

fml*_*ml2 5 spring-boot spring-boot-test

我有一个基于 SpringBoot 的命令行应用程序。应用程序在数据库中创建或删除一些记录。它不是直接通过 JDBC 而是通过特殊的 API(实例变量dbService)来执行此操作。

应用程序类如下所示:

@SpringBootApplication
public class Application implements CommandLineRunner {

  @Autowired
  private DbService dbService;
  
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
  
  @Override
  public void run(String... args) {
    // Do something via dbService based on the spring properties
  }

}
Run Code Online (Sandbox Code Playgroud)

现在我想创建一个 SpringBoot 测试,该测试将使用专门为测试准备的配置来运行整个应用程序。

我使用内存数据库(H2)运行测试,该数据库在测试开始时为空。因此,我想将一些记录插入数据库——作为测试的设置。必须执行插入记录的代码

  1. 在加载 Spring 上下文之后——以便我可以使用该 bean dbService

  2. 在应用程序运行之前——以便应用程序使用准备好的数据库运行。

不知怎的,我未能落实以上两点。

到目前为止我所拥有的是这样的:

@SpringBootTest
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
@ActiveProfiles("specialtest")
public class MyAppTest {
 
  @Autowired
  private DbService dbService;
  
  private static final Logger logger = LoggerFactory.getLogger(MyAppTest.class);
 
  // The expectation is that this method is executed after the spring context
  // has been loaded and all beans created, but before the Application class
  // is executed.
  @EventListener(ApplicationStartedEvent.class)
  public void preparedDbForTheTest() {
    // Create some records via dbService
    logger.info("Created records for the test");
  }
 

  // This test is executed after the application has run. Here we check
  // whether the DB contains the expected records.
  @Test
  public void testApplication() {
    // Check the DB contents
  }
 
}
Run Code Online (Sandbox Code Playgroud)

我的问题是该方法preparedDbForTheTest似乎根本没有被执行。

根据SpringBoot 文档,该事件ApplicationReadyEvent是在我想要执行设置代码时发送的。但不知怎的,代码没有被执行。

如果我用(我尝试了它的几种变体)注释该方法@Before...,那么它会被执行,但在Application 类运行之后。

我究竟做错了什么?

And*_*son 6

测试类不是 Spring 管理的 bean,因此@EventListener方法之类的内容将被忽略。

解决您的问题的最传统的解决方案是添加一些@TestConfiguration声明@EventListener

@SpringBootTest
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class MyAppTest {
  
  private static final Logger logger = LoggerFactory.getLogger(MyAppTest.class);
 
  @Test
  public void testApplication() {
  }
  
  @TestConfiguration
  static class DatabasePreparation {
    @EventListener(ApplicationStartedEvent.class)
    public void preparedDbForTheTest() {
      logger.info("Created records for the test");
    }
  }
 
}
Run Code Online (Sandbox Code Playgroud)

A@TestConfiguration是附加的,因此它将与应用程序的主配置一起使用。现在将调用该preparedDbForTheTest方法作为刷新测试的应用程序上下文的一部分。

请注意,由于应用程序上下文缓存,不会为每个测试调用此方法。它只会作为刷新上下文的一部分被调用,然后可以在多个测试之间共享。