Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Is there any framework which just rollback all the transactions after each test finished so every test will have fresh/same DB data to Test. And this way after all Test executions, data of DB schema would be as it is.</p> </blockquote> <p>From <a href="https://stackoverflow.com/questions/6936845/clear-the-in-memory-database-after-every-testcase/6936962#6936962">my other answer</a> posted earlier in the day, yes, this is possible using DbUnit. (Based on your edit, you don't need this; the subsequent section of my answer addresses why I use DbUnit, and when I wouldn't use it).</p> <p>The following code snippet demonstrates how the setup of every test is performed:</p> <pre><code>@Before public void setUp() throws Exception { logger.info("Performing the setup of test {}", testName.getMethodName()); IDatabaseConnection connection = null; try { connection = getConnection(); IDataSet dataSet = getDataSet(); //The following line cleans up all DbUnit recognized tables and inserts and test data before every test. DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet); } finally { // Closes the connection as the persistence layer gets it's connection from elsewhere connection.close(); } } private IDatabaseConnection getConnection() throws Exception { @SuppressWarnings({ "rawtypes", "unused" }) Class driverClass = Class.forName("org.apache.derby.jdbc.ClientDriver"); Connection jdbcConnection = DriverManager.getConnection(jdbcURL, "XXX", "YYY"); IDatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection); return databaseConnection; } private IDataSet getDataSet() throws Exception { ClassLoader classLoader = this.getClass().getClassLoader(); return new FlatXmlDataSetBuilder().build(classLoader.getResourceAsStream("database-test-setup.xml")); } </code></pre> <p>The <code>database-test-setup.xml</code> file contains the data that will be inserted into the database for every test. The use of <code>DatabaseOperation.CLEAN_INSERT</code> in the <code>setup</code> method ensures that all the tables specified in the file will be cleared (by a delete of all rows) followed by an insert of the specified data in the test data file.</p> <p><strong>Avoiding DbUnit</strong></p> <p>I use the above approach specifically to clear out sequences before the start of every test, as the application uses a JPA provider which updates the sequences in a separate transaction. If your application is not doing anything like that, then you can afford to simply start a transaction in your <code>setup()</code> method and issue a rollback on teardown after the test. If my application didn't use sequences (and if I didn't desire to reset them), then my setup routine would have been as simple as:</p> <pre><code>@Before public void setUp() throws Exception { logger.info("Performing the setup of test {}", testName.getMethodName()); // emf is created in the @BeforeClass annotated method em = emf.createEntityManager(); // Starts the transaction before every test em.getTransaction.begin(); } @After public void tearDown() throws Exception { logger.info("Performing the teardown of test {}", testName.getMethodName()); if (em != null) { // Rolls back the transaction after every test em.getTransaction().rollback(); em.close(); } } </code></pre> <p>Also, I use <a href="http://dbdeploy.com/" rel="nofollow noreferrer">dbdeploy</a> with Maven, but that is primarily for keeping the test database up to date with the versioned data model.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload