springtestdbunit / spring-test-dbunit

Integration between the Spring testing framework and DBUnit
http://springtestdbunit.github.com/spring-test-dbunit/
Apache License 2.0
476 stars 238 forks source link

The DBUnit dataset has not yet been imported when the ContextRefreshedEvent if fired #136

Open HereThereBeMonsters opened 7 years ago

HereThereBeMonsters commented 7 years ago

I have a Spring context event listener, that I use to run some initializing code for some services. That code needs a transactional context so it is not possible to run it in a @PostConstruct method.

The problem is that when the onApplicationEvent method is executed, Liquibase has not yet been executed, and the (in-memory) database is empty.

Is there a way to force the execution of Liquibase earlier in the Spring life cycle ?

Here is the code of my listener:

@Component
public class Initializer implements ApplicationListener<ContextRefreshedEvent> {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private QueuesRegistry queuesRegistry;

    private TicketNumberGeneratorRegistry ticketNumberGeneratorRegistry;

    public Initializer(QueuesRegistry queuesRegistry, TicketNumberGeneratorRegistry ticketNumberGeneratorRegistry) {
        this.queuesRegistry = queuesRegistry;
        this.ticketNumberGeneratorRegistry = ticketNumberGeneratorRegistry;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        logger.info("The application has started. Now initializing the state of some components...");
        queuesRegistry.initialize();
        ticketNumberGeneratorRegistry.initialize();
    }
}