简体   繁体   中英

dbunit how to sync real database with test database

I have an application pointing to a mysql database.

I have been trying to use DBUnit to set up my tests environments, which works fine.

The problem is that when configuring DBUnit I pointed it to the SAME mysql database. So when DBUnit is executed, it takes the specified dataset.xml and overrides the information from my original database. which makes sense because there is where I am pointing it to.

The question is, am I supposed to create a new database only for tests so my DBUnit can point to it? If so, how would I manage the structure synchronization between my original database and the one for tests?

Thanks in advance.

am I supposed to create a new database only for tests so my DBUnit can point to it?

It is a better approach to do so as it eliminates multiple problems.

how would I manage the structure synchronization between my original database and the one for tests?

You don't mention tech in your persistence stack, such as Hibernate, Spring/Spring Boot/Spring Data/Flyway/LiquiBase/etc. to suggest more of how to implement this. In general, run DDL in the schema at tests run startup (either from managed DDL from something like Flyway or auto-generation from Hibernate).

Additionally, my preferred and typical testing approach is with:

  1. An in-memory/embedded database for its speed, such as Apache Derby, automatically started just before launching tests.
  2. Create tables in schema using Hibernate DDL gen from annotated entities.
  3. No existing rows in any tables; happens automatically with an embedded database and a clean build when storing any of its files in a subdirectory of the build output dir.
  4. dbUnit configured with DatabaseOperation.CLEAN_INSERT [0].
  5. Minimal dbUnit data for each test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM