Cleaning the Database with Transactions
This is a clever approach that uses the database’s transaction support in a somewhat unusual way. Before the scenario starts, we start a new database transaction in a @Before hook. Then, our step definitions and application insert and modify data in the database. However, since this is happening in an uncommitted transaction, nothing gets changed in the database until the transaction is committed. Then, when the scenario is over (in an @After hook), we do the opposite. We roll the transaction back! All the data that was modified during the scenario gets lost, and the database is back in its original state. You can see how this works in Figure 4, Database transactions.