JUnit test cases - data setup

I have a simple CRUD operation that needs to be checked by the module. These test cases relate to the DAO layer, so all tests relate to the database and therefore cannot be mocked.

So, I have one test case for creating another for updating, and another for reading.

  • Should I hard-code the data in the JUnit class or externalize it?

  • Reading TestCase obviously requires data in the database. Do I have to depend on Create Test scripts to set up the data or use an SQL statement to do this?

What is the best practice?

If you can point me to an online resource that discusses this, that would be great.

+3
source share
2 answers

Spring has excellent support for these kinds of things - unit tests that you want to use against a β€œtest” database, which can be overwritten for each individual unit test.

The last part of this last sentence is the key to developing reusable and extensible unit tests - a unit test should not be forced to rely on data optimistically in a certain state, or rely on the previous unit test to run first - you will need to re-create the database for each unit test so that each test case receives a "clean" version of the data.

Spring MVC , , , Spring MVC - , , / script, Spring ..

+5

DBUnit. . .

, tearDown().

+2

Source: https://habr.com/ru/post/1706489/


All Articles