Test, if I don’t want to trigger the whole thing

If you want to avoid that the whole Spring Context is started for all of your integration tests, you can take a look at other test annotations that create a sliced context:

  • @WebMvcTest creates a Spring Context with only MVC related beans
  • @DataJpaTest creates a Spring Context with only JPA/JDBC related beans
  • etc.

In addition to this, I also would remove your CommandLineRunner from your main entry Spring Boot entry point class. Otherwise also the annotations above would trigger the logic.

Therefore you can outsource it to another @Component class:

public class WhateverInitializer implements CommandLineRunner{

  private ApplicationEventPublisher publisher;

   // ...

  public void run(String... args) throws Exception {
     // read data from a file and publishing an event


Apart from this you can also use @Profile("production") on your Spring beans to only populate them when a specific profile is active. This way you can either include or exluce them for all your integration tests if you don’t want e.g. this startup logic always.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top