How to load DBUnit test data once per case with Spring Test

前端 未结 4 1655

Spring Test helpfully rolls back any changes made to the database within a test method. This means that it is not necessary to take the time to delete/reload the test data

4条回答
  •  抹茶落季
    2020-12-06 11:34

    Methods annotated with @BeforeTransaction run, like its name suggests, before the transaction of each test is started. If in such method you can detect if the test data is loaded, then one can load the data when needed.

    Beware though that the data is left in your (in-memory) database for all subsequent tests.

    We use this to load "static" data that would, in a production environment, also be bootstrapped into our database when starting it. This way we actually use exactly the same code and data for our tests, rather than relying on (DbUnit) exports that might become outdated.

提交回复
热议问题