Search code examples
javaspring-boottestngspring-transactionstestng-dataprovider

Database updates made in TestNG @DataProvider not rolling back


In my automated tests, I am wanting to use a @DataProvider to 1.) perform some database updates, in order to guarantee the state of the data that I am about to test, and 2.) return the test data that will work in such an environment.

What I am finding is that the updates are made successfully in the @DataProvider body, then the updates made in my test are successful, all assertions pass, and then the testing updates are rolled back. However, the @DataProvider updates are NOT rolled back and are left after the test has completed.

Is there a way to roll back database updates made inside of a @DataProvider body?


Solution

  • You question is simillar to How to delete data only after all tests, based on inputs from data provider, are run?

    It's answer is applicable for you.


    UPDATE

    In your case @DataProvider is a bad option. In TestNG you can work only with results provided by @DataProvider and they are running before suite. You have several ways to solve your problem.

    • Create a listener which is connecting DB transactions with related @DataProvider result. It has to implement ITestListener. On each test finish event your listener will rollback required transactions.
    • Second option, is like the first one, but your @DataProvider does nothing with DB, it's just provide some inputs, which should be handled by @BeforeMehtod and @AfterMethod, updated and rollbacked respectively. In that case all your context could be stored in particular test class.
    • Also you can create a new annotation that using principles descibed above, providing and managing your buisness entities.