How to rollback nested transactions with Propagation.REQUIRES_NEW in integration tests

后端 未结 2 891
挽巷
挽巷 2021-02-02 11:43

I have several integration tests for various services that extend the following baseclass:

@ContextConfiguration(locations=\"classpath:applicationContext-test.xm         


        
2条回答
  •  猫巷女王i
    2021-02-02 12:28

    This is expected behaviour, and is one of the main reasons to use REQUIRES_NEW :

    • be able to rollback the new transaction, but commit the outer one
    • be able to commit the new transaction, but rollback the outer one

    re-populate the database between tests is probably the best solution, and I would use this solution for all the tests: this allows tests to check that everything works correctly, including the commit (which could fail due to flushing, deferred constraints, etc.).

    But it you really want to rollback the transaction, a solution would be to add a boolean argument rollbackAtTheEnd to your service, and rollback the transaction if this argument is true.

提交回复
热议问题