Konrad Garus
Konrad Garus

Reputation: 54005

Spring/Hibernate testing - how to drop schema when done?

I am trying to learn integration testing with Spring and Hibernate. I would like it to create schema before running tests and drop it when tests finish so that they always start clean.

hibernate.hbm2ddl.auto=create-drop does the first part for me: Create schema. But how do I get it to drop it?

I am testing it by deploying my application to JBoss. Schema is created properly, but when I undeploy or stop server it is not dropped. How can I do it?

Upvotes: 1

Views: 1007

Answers (4)

TrueDub
TrueDub

Reputation: 5070

You could use something like DBUnit to control the creation & deletion of database tables.

Upvotes: 0

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298838

One way would be to register a custom bean in your applicationContext that implements ApplicationListener<ContextClosedEvent> to receive shutdown events, inject a JdbcTemplate into that bean and let it call DROP SCHEMA ... or whatever you want it to do.

Upvotes: 2

kandarp
kandarp

Reputation: 5047

you should write listener which implements ServletContextListener interface. When you implement this interface, you need to override contextInitialized and contextDestroyed methods. In ContextDestroyed method, you can write your drop schema code.

Upvotes: 0

Fixus
Fixus

Reputation: 4641

If I remember correct it works like this

First deploy: Creates new tables from schema

Second deplot: Drops old tables and creates

Upvotes: 0

Related Questions