Is it possible with Spring Boot Test to set a conditional execution of sql scripts depending on the active profile?
I mean, I have my integration tests for repositories annotated with some @sql annotations like:
@Sql(scripts = "/scripts/entity_test_clear.sql",
executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
For a profile
h2I want to execute entity_test_clear.sqlFor a profile
mysqlI want to execute entity_test_clear_mysql.sql
The reason for it is that I use different syntax for these databases, particularly this one:
ALTER TABLE organisation ALTER COLUMN org_id RESTART WITH 1;ALTER TABLE organisation AUTO_INCREMENT = 1;
Mysql doesn't understand the syntax #1, while h2 doesn't understand the syntax #2 (despite the mysql mode set, like MODE=MYSQL)
By default, I use h2 for IT tests, but also, in some rarer cases, I would like to check everything works smoothly with Mysql too.
P.S I could of course try a straight-forward solution with @Profile and hard code two copies of each test for h2 and Mysql, but it is coupled with huge code duplication in tests, which I would like to avoid.
EDITED:
The test case looks like this:
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
public class EntityRepositoryTestIT {
@Autowired
private EntityRepository entityRepository;
@Test
@Sql(scripts = {"/scripts/entity_test_data.sql", "/scripts/entity_test_data_many.sql"},
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/scripts/entity_test_clear.sql",
executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void findTest() {
Page<Entity> e = entityRepository.findBySomeDetails(1L, PageRequest.of(0, 20));
Assert.assertEquals(3, e.getContent().size());
Assert.assertEquals(1, e.getContent().get(0).getResources().size());
// more asserts
}
Thank you for any suggestions!