1

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?

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

If I remember correct it works like this

First deploy: Creates new tables from schema

Second deplot: Drops old tables and creates

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.