John Manak
John Manak

Reputation: 13558

Database cleanup after Selenium tests

After I run a bunch of Selenium (jUnit4) tests using Maven, I'd like to do a database clean-up (remove things they inserted etc). It's an older project running on Tapestry/Spring/Hibernate and a legacy database. I'd like to do the clean up in an @After annotated method - but injecting of DAO's/Managers/SessionFactory doesn't work.

The testing goes like this: I run (mvn jetty:run-war) the app in one console, and start the testing in another console (mvn test) - it accesses the app on localhost:8080.

Upvotes: 1

Views: 4412

Answers (2)

Heril Muratovic
Heril Muratovic

Reputation: 2028

I would rather use

@Transactional

anotation above method declaration. It runs rollback after each test. It works fine for me.

For example:

@Test
@Transactional  
public void simpleTest(){    
    // your logic here     
}

Upvotes: 0

orangepips
orangepips

Reputation: 9971

Several possible approaches:

  1. Use dbunit, which is designed to return a database to a know state between tests.
  2. Wrap each test in a database transaction with a try{} finally{} block where the finally rolls back the transaction.
  3. Use a database strictly for testing and don't worry about it. Make your tests create uniquely identified / named values each time so you don't have conflicts, but otherwise don't take any action.

Upvotes: 6

Related Questions