Bernardo-MG / painting-journal

Create and save paint palettes for your miniatures
Apache License 2.0
0 stars 0 forks source link

Test SQL scripts are commited #5

Closed Bernardo-MG closed 5 years ago

Bernardo-MG commented 5 years ago

The test SQL scripts should be rolled back after the test, but they aren't. This breaks other tests, as there is additional data.

The logs show that it is being commited:

DEBUG | 2019-10-01T21:47:30,383 | o.s.o.h.HibernateTransactionManager getTransaction 372 | Creating new transaction with name [com.bernardomg.tabletop.palette.test.integration.palette.service.ITPaletteServiceRead.testRead]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
DEBUG | 2019-10-01T21:47:30,384 | o.s.o.h.HibernateTransactionManager doBegin 465 | Opened new Session [SessionImpl(1094838264<open>)] for Hibernate transaction
DEBUG | 2019-10-01T21:47:30,403 | o.s.o.h.HibernateTransactionManager doBegin 552 | Exposing Hibernate transaction as JDBC [org.springframework.orm.hibernate5.HibernateTransactionManager$$Lambda$555/1475293782@5350e389]
DEBUG | 2019-10-01T21:47:30,436 | o.s.j.d.i.ScriptUtils executeSqlScript 459 | Executing SQL script from class path resource [db/palette_group.sql]
DEBUG | 2019-10-01T21:47:30,440 | o.s.j.d.i.ScriptUtils executeSqlScript 491 | 1 returned as update count for SQL: INSERT INTO palette_groups (id, name) VALUES (1, 'Group1')
DEBUG | 2019-10-01T21:47:30,441 | o.s.j.d.i.ScriptUtils executeSqlScript 525 | Executed SQL script from class path resource [db/palette_group.sql] in 3 ms.
DEBUG | 2019-10-01T21:47:30,441 | o.s.o.h.HibernateTransactionManager processCommit 743 | Initiating transaction commit
DEBUG | 2019-10-01T21:47:30,441 | o.s.o.h.HibernateTransactionManager doCommit 620 | Committing Hibernate transaction on Session [SessionImpl(1094838264<open>)]
DEBUG | 2019-10-01T21:47:30,444 | o.s.o.h.HibernateTransactionManager doCleanupAfterCompletion 726 | Closing Hibernate Session [SessionImpl(1094838264<open>)] after transaction
Bernardo-MG commented 5 years ago

Seems related to this problem:

https://stackoverflow.com/questions/9673815/executing-sql-script-using-spring-test-is-committing-changes

Bernardo-MG commented 5 years ago

One recomendation there is disabling the autocommit option in the datasource. But that didn't work.

Bernardo-MG commented 5 years ago

Spring documentation: https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#testcontext-executing-sql-declaratively

Bernardo-MG commented 5 years ago

The annotations were configured incorrectly. Apparently it was related to the listener and automatic transaction detection.

The correct configuration is:

@RunWith(JUnitPlatform.class)
@SpringJUnitConfig
@ContextConfiguration(
        locations = { "classpath:context/application-context.xml" })
@Transactional
@Rollback
@Sql({ "/db/palette_group.sql" })