0

I'm in the process of creating unit tests for our Spring Boot REST API with Hibernate. I'm just wondering if it's a problem when I use @Transactional. Do I see it right that if I annotate the test with @Transactional, the data will never really end up in the database and a potential source of error will be bypassed? Since certain errors only occur during commit? Or do I see it wrong?

Rollback transaction after @Test - The following question does not quite correspond to my question. Because I wanted to know how to trigger a commit and still do a rollback. What is possible with EntityManager.flush(). I know how to reset the database after each test.

Sven M.
  • 587
  • 7
  • 23
  • 3
    Possible duplicate of [Rollback transaction after @Test](https://stackoverflow.com/questions/12626502/rollback-transaction-after-test) – Kamil W Aug 29 '19 at 13:02
  • 1
    Also, if it's a unit test you might want to use a mock database, so you have a fine-grained control over the data. That really depends on what you want to test... – Filippo Possenti Aug 29 '19 at 15:03
  • I had already looked at the question, but I had another problem. I didn't know anything about EntityManager and that I can trigger database validation with flush(). This makes a commit superfluous and I can still use @Transactional. But thank you. =) – Sven M. Aug 29 '19 at 19:53

2 Answers2

1

In case of transaction management is enabled in test configuration, for commit at the end of test method execution in spring are present @Commit and of course @Rollback for rollback at the end. For manual transactions controlling i use TestTransaction.start() and TestTransaction.end() especially for some delete and update method to be sure and checking results of action.(TestTransaction required @Commit annatation on method or manually setting TestTransaction.flagForCommit())

borino
  • 1,720
  • 1
  • 16
  • 24
  • Oh, thank you. I didn't know `TestTransaction` before. That's very practical. But does the database have to empty you manually afterwards, if I see it correctly? – Sven M. Aug 30 '19 at 06:31
  • 1
    Of course in case of data was committed you should clean your database, if required. – borino Aug 30 '19 at 06:55
  • Okay, well, that's a problem from my point of view, too. Since it is relatively cumbersome to clean up the database every time. Apparently `EntityManager.flush()` triggers the same actions as a commit? Except it doesn't end up in the database, if I understood that correctly. – Sven M. Aug 30 '19 at 07:01
  • 1
    For my opinion test should be written with subject to availability of data in the database. For example if you create some entity for empty database , and at the end verify it like `someRepository.count() == 1` i think it is not right. For this purpose i do `beforeTestQuantity = someRepository.count()` , and at the end verify results `someRepository.count() == beforeTestQuantity + 1`. And for fully sure test result i prefer commit data instead of flushing. – borino Aug 30 '19 at 07:14
  • Hmmm ... Right, I had never thought about it that way. I think you're right. I think I approached testing the wrong way. – Sven M. Aug 30 '19 at 07:29
0

There's no need for a committal. Because EntityManager.flush() triggers the same actions.

Sven M.
  • 587
  • 7
  • 23