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

标签 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
  }

}

现在我想创建一个 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
  }
 
}

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

根据SpringBoot docs ,事件 ApplicationReadyEvent 恰好在我想要执行设置代码时发送。但是不知何故代码没有被执行。

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

我做错了什么?

最佳答案

测试类不是 Spring 管理的 beans,所以像 @EventListener 方法这样的东西将被忽略。

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

@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");
    }
  }
 
}

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

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

关于spring-boot - 如何在应用程序运行之前在 SpringBootTest 中执行代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71087509/

相关文章:

java - 无法构建具有从 jndi 查找数据源的 Spring Boot 项目

maven - pom文件中可以指定多个主类吗?

reactjs - 如何读取 ReactJs 应用程序中的 Spring Boot application.properties?

java.sql.SQLIntegrityConstraintViolationException : Duplicate entry '0' for key 'PRIMARY'

spring-boot - 将 Spring Cloud Contract Stub Runner 与手动创建的 stub 结合使用

junit - 如何在 spring-boot 中使用 resttemplate 模拟调用另一个服务的服务?

spring-boot - 如何在 SpringBootTest 中使用 Kotlin beans dsl 初始化程序

java - 我们应该在 Spring Boot 中为存储库层编写单元测试和集成测试吗?

java - Spring Boot中的一个简单的REST项目是否使用了MVC原理?

java - 在 Spring Config Server 中添加环境存储库