How do I reset my database state after each unit test without making the whole test a transaction?

前端 未结 6 1829
心在旅途
心在旅途 2020-12-16 09:50

I\'m using Spring 3.1.1.RELEASE, Hibernate 4.1.0.Final, JPA 2, JUnit 4.8.1, and HSQL 2.2.7. I want to run some JUnit tests on my service methods, and after each test, I wou

6条回答
  •  遥遥无期
    2020-12-16 10:32

    Since you are using hibernate, you could use the property hibernate.hbm2ddl.auto to create the database on startup every time. You would also need to force the spring context to be reloaded after each test. You can do this with the @DirtiesContext annotation.

    This might add a bit extra overhead to your tests, so the other solution is to just manually delete the data from each table.

提交回复
热议问题