-1

I want to prepare SQL data once per Spec and remove it after Spec is completed. It should be done by Spring service

As I understand I can do following:

  • I can do it in setup: but it will lead to too many useless inserts/rollbacks (if I've got few data-driven tests)
  • I can use @BeforeTransaction (check this example) - how to clear data after test is completed?
fedor.belov
  • 22,343
  • 26
  • 89
  • 134

1 Answers1

0

Annotate your test method with @Test and @Transactional and it will automatically roll back when the test is complete.

th3morg
  • 4,321
  • 1
  • 32
  • 45
  • This is exactly what I don't want to achieve. It will rollback on every test iteration. I need to rollback data only once - after spec is completed – fedor.belov Mar 21 '15 at 11:33
  • It is considered best practice to not share data between tests because you could end up relying on the order of execution or a newly introduced test could break another one in the same class. It is more fragile that way and the cost of creating and wiping data for each test is minimal. – th3morg Mar 21 '15 at 15:29
  • This method interferes with any @Transactional methods you may have, such as in a service, so testing that those roll back correctly will not work properly – chrismacp Sep 28 '17 at 11:43