How to re-create database before each test in Spring? How to re-create database before each test in Spring? java java

How to re-create database before each test in Spring?


Actually, I think you want this:

@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)

http://docs.spring.io/autorepo/docs/spring-framework/4.2.6.RELEASE/javadoc-api/org/springframework/test/annotation/DirtiesContext.html

@DirtiesContext may be used as a class-level and method-levelannotation within the same class. In such scenarios, theApplicationContext will be marked as dirty after any such annotatedmethod as well as after the entire class. If theDirtiesContext.ClassMode is set to AFTER_EACH_TEST_METHOD, the contextwill be marked dirty after each test method in the class.

You put it on your Test class.


Using the accepted answer in Spring-Boot 2.2.0, I was seeing JDBC syntax errors related to constraints:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Constraint "FKEFFD698EA2E75FXEERWBO8IUT" already exists; SQL statement: alter table foo add constraint FKeffd698ea2e75fxeerwbo8iut foreign key (bar) references bar [90045-200]

To fix this, I added @AutoConfigureTestDatabase to my unit test (part of spring-boot-test-autoconfigure):

import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;import org.springframework.test.annotation.DirtiesContext;import org.springframework.test.annotation.DirtiesContext.ClassMode;import org.springframework.boot.test.context.SpringBootTest;import org.junit.runner.RunWith;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTest@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)@AutoConfigureTestDatabase(replace = Replace.ANY)public class FooRepositoryTest { ... }


To create the database you have to do what the other answers say with the spring.jpa.hibernate.ddl-auto=create-drop, now if your intent is to pupulate the database on each test then spring provides a very usefull anotation

@Transactional(value=JpaConfiguration.TRANSACTION_MANAGER_NAME)@Sql(executionPhase=ExecutionPhase.BEFORE_TEST_METHOD,scripts="classpath:/test-sql/group2.sql")public class GroupServiceTest extends TimeoffApplicationTests {

that is from this package org.springframework.test.context.jdbc.Sql; and you can run a before test method and a after test method. To populate the database.

Regarding creating the database each time, Say you only want your Test to have the create-drop option you can configure your tests with a custom properties with this annotation

@TestPropertySource(locations="classpath:application-test.properties")public class TimeoffApplicationTests extends AbstractTransactionalJUnit4SpringContextTests{

Hope it helps