Hello,

I've asked a question on StackOverflow but got no answers. Since I consider the behaviour a bug, I'm opening this ticket.

A short description of the issue:

  • I have a CommandLineRunner Spring Boot application
  • I have a SpringBootTest for the application
  • In the test, I want some code to be executed after the spring context has been loaded and beans created but before the command line runner is run.
  • According to the SpringBoot docs, the event ApplicationReadyEvent is sent exactly when I want to execute the setup code.

The issue is that the method annotated with @EventListener(ApplicationReadyEvent.class) is not get executed. The method is defined in the test class.

The test class looks like this:

@SpringBootTest
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
@ActiveProfiles("specialtest")
public class MyAppTest {

  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(ApplicationReadyEvent.class)
  public void preparedDbForTheTest() {
    // Perform some actions
    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
  }

}

I'm using SpringBoot 2.5.9.

I've also read the answer to the question SpringBoot EventListener don't receive events, but in my case, I use an event that's sent reasonably late in the application life cycle.

Comment From: wilkinsona

This is working as designed. I'll post an answer on Stack Overflow.