How to rollback a database transaction when testing services with Spring in JUnit?

前端 未结 4 418
有刺的猬
有刺的猬 2020-12-01 03:19

I have no problem testing my DAO and services, but when I test INSERTs or UPDATEs I want to rollback the transaction and not effect my database.

4条回答
  •  遥遥无期
    2020-12-01 04:10

    Use Following annotation before class :

    @TransactionConfiguration(transactionManager = "txManager",defaultRollback = true)
    @Transactional
    

    here txManager is application context's Transaction Manager.

    Here txManager is an instance or bean id of Transaction manager from application context.

    
        
            
        
    
        
    

    Add your code inside setUp() method, this will execute in start of the test and the last wrap up code should be put in teatDown() method that will executed at last. or you can also use @Before and @After annotation instead of it.

提交回复
热议问题