I'm working with an older test setup where tests impacts each other and I'm trying to resolving this issue. I figured truncating all tables between the tests would do it but I can't seem to get the EM to play nicely with the command, this is how the relevant part of the code looks:
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(...)
@ContextConfiguration(...)
public class MyTest {
@PersistenceContext
private EntityManager em;
@After
@Transactional // Current suite can't handle all tests being transactional
public void after() {
// ERROR: Method is not allowed for a query. Use execute or executeQuery instead of executeUpdate
em.createNativeQuery("SELECT CONCAT('TRUNCATE TABLE ', TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES").executeUpdate();
}
}
I understand that using executeUpdate() this way is a no-go but what options do I have that doesn't lead to me having to copy-paste a ton of rows where I do the delete per table explicitly?
I found a workaround to the issue, not the prettiest but at least works: