Spring MVC - Flushing Database Between Tests Using Span

I use Flyway to control the state of my database in my Spring MVC application.

I configured it in the servlet context XML file exactly as I recommend in my docs

<bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">
    <property name="dataSource" ref="..."/>
    ...
</bean>

<!-- The rest of the application (incl. Hibernate) -->
<!-- Must be run after Flyway to ensure the database is compatible with the code -->
<bean id="sessionFactory" class="..." depends-on="flyway">
    ...
</bean>

I would like to do two things in JUnit tests -

  • Once before ALL tests, drop and recreate the database and let it re-migrate. This creates a clean database for each test suite.

  • Before each test, clear all database tables. In other frameworks (e.g. RSpec / Rails), I accomplished this by executing database transactions transactionally so that they roll back at the end of the test. Not sure what is the best in the Spring MVC world.

I have no clue where to really start by implementing the above, so any guidance is appreciated.

!

+4
2

, :

@Autowired
 Flyway flyway;

@Before
puublic void init(){
   flyway.clean();
   flyway.migrate();
}

-, JdbcTestUtils . , : JDBC https://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html

@Rollback @Commit

@Rollback , . true, ; (. @Commit). Spring TestContext Framework true, @Rollback .

+8

SpringBoot. Flyway. , , SpringBoot, SpringBoot .

Flyway Spring Test H2. Spring Profiles H2 JDBC. Spring Test . , , Flyway, . , H2 . , .

Flyway/ Spring , , , Flyway .

+2

Source: https://habr.com/ru/post/1668221/


All Articles