JUnit tests always rollback the transactions

后端 未结 7 1220
死守一世寂寞
死守一世寂寞 2020-12-05 02:54

I\'m running a simple JUnit test agains an application DAO. The problem is that I always get:

javax.persistence.RollbackException: Transaction marked as roll         


        
相关标签:
7条回答
  • 2020-12-05 03:03

    It is strange to desire a test that changes your database and keep the modification. Tests are supposed to be orthogonal : no test depends on an other. Moreover, tests are supposed to be independent of tests order, and even idempotent.

    So either you want to change you data base in your setUp() method and rollback the change in your tearDown() method, either you want to setup a test database with some good values in it for tests.

    Maybe I am missing something here but usually you should not want that.

    0 讨论(0)
  • 2020-12-05 03:10

    I agree the Ralph's answer.

    The Propagation.REQUIRES_NEW creates a new transaction and this probably does not match with the main transactional route in which the test is running.

    In my simple experience the annotation @Transactional will properly work to define the transactional context in which every single test should run, delegating to this one the specific current Rollback clause (as shown by Ralph).

    The Ralph's answer is useful and in the same time the Snicolas's answer concerns a particular case of managing context of tests. The idempotence is fundamental for integration and automatic tests, but should be different ways to implements them. The question is, which kind of methods do you have? And what behavior do theese methods have?

       [...]
       @Transactional
    
       public class Test {
    
       @Test
       @Rollback(false)
       public void test() {
    
       [...]
    

    Is the simple, question-coherent way :)

    0 讨论(0)
  • 2020-12-05 03:12

    Just add annotation Rollback and set the flag to false.

       @Test
       @Rollback(false)
    
    0 讨论(0)
  • 2020-12-05 03:19

    From official Documentation:

    By default, test transactions will be automatically rolled back after completion of the test; however, transactional commit and rollback behavior can be configured declaratively via the @Commit and @Rollback annotations

    https://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#integration-testing-annotations

    @Commit indicates that the transaction for a transactional test method should be committed after the test method has completed. @Commit can be used as a direct replacement for @Rollback(false) in order to more explicitly convey the intent of the code.

    0 讨论(0)
  • 2020-12-05 03:20

    It should work, like you expect it, but may be you open another transaction within your class under test or you have an other feature/or bug somewhere.

    Btw this annotations should be enougth:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:com/my/app/context.xml"}
    @Transactional
    public class PerformanceTest {
    
        @Test
        @Rollback(false)
        public void testMsisdnCreationPerformance() {
            // Create a JPA entity
    
            // Persist JPA entity
        }
    }
    

    @See Spring Reference Chapter 9.3.5.4 Transaction management

    0 讨论(0)
  • 2020-12-05 03:26
    1. add @Rollback on your Test class-level

    2. add @Transactional(value = "your_ManagerName",rollbackFor = Exception.class) on your test method

    0 讨论(0)
提交回复
热议问题