Alex Luya
Alex Luya

Reputation: 9922

How to prevent test from cleaning up db before each test in TestNG?

I have a test like this:

 @RunWith(SpringJUnit4ClassRunner.class),
 @ContextConfiguration(locations = { "file:war/WEB-INF/application-context.xml" })
 @Transactional
 public class ServiceImplTest extends AbstractTestNGSpringContextTests
 {
     @Autowired
     private Service service;

     @Test
     @Rollback(false)
     public void testCreate()
     {
         .....
         //save an entity to table_A
         service.save(a);
     }
 }

It seems that the table_A will be cleaned up before each test running(not roolback after test ran),because after each test,all old data entries in the table are cleaned up,only new inserted entry by test is left.How to prevent this "cleaning" action?

Upvotes: 0

Views: 238

Answers (1)

Vikdor
Vikdor

Reputation: 24124

The default behavior is to rollback the transactions in testing context. You can override this behavior using the @Rollback(false) annotation on a test method to not rollback the changes made to the DB during that particular test.

That said, it is recommended that each test case is independent and should have its own scenario setup, scenario execution and scenario tear down. Otherwise, the test failure behavior would be difficult to analyze if there are inter-dependencies among tests.

Upvotes: 1

Related Questions