Java – JUnit test case – set data

I have a simple crud operation that needs unit testing

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? > Read testcase obviously needs data in the database Should I rely on creating test cases to set up data or use SQL statements?

What is the best practice?

It would be great if you could point out the Internet resources I discussed this

Solution

Spring has excellent support for this sort of thing – for unit tests that operate on the "test" database, you can write scripts to recreate them on each individual unit test

The second half of the last sentence is the key to developing reusable and extensible unit tests - unit tests on databases should not be forced to rely on optimistic data in a specific state, or rely on previous unit tests to run first - you need to recreate the database for each unit test so that each test case can get a "clean" version of the data

The step-by-step tutorial on setting up spring MVC is actually has a section on setting up unit tests for database classes. I think even if you don't use spring MVC, it is also a valuable reference - you can use it as a reference for how to set up the test database to be created / initialize from the build script, use spring container to reload data during each test run, etc

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>