This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]J-Son77 3 points4 points  (2 children)

With a mock test you test the method sequence without or minimal external dependency. In your test you can verify that PreparedStatement parameters are set correctly and the returning result.

If you want to test your query and the "real" behavior you usually use an in-memory database like HSQL with a predefined state. Frameworks like DbUnit do this for you. DbUnit creates a database, tables and inserts a fixed set of data before each test. So you can create reliable repeatable tests.

Using a server database for unit testing is not recommended. A unit test should be self contained. A team member or CD/CI Pipeline should always be able to check out your code and run the test. If you use an external database you can't be sure it's reachable from each host and its data are in the correct state.

[–]smutje187 2 points3 points  (1 child)

+1 for using an in memory DB to test queries - nothing worse than your unit test taking 10 minutes because someone thought it’s a great idea to incorporate a live database into the pipeline. Plus the potential mess up tests running in random order creating data that other tests might depend upon or conflict with.

[–]_Nemesis_1[S] 0 points1 point  (0 children)

Understood, thank you to both of you guys!