I’m developing the web Java application and come across the following issue:
I’m using jUnit 4.5.
Suppose that I have a method which aggregates data from a database for the rendering their to a markup. Is it make a sense to testing an sql query in the way that I simple write a series of sql-queries and compare expected and real results?
1
A unit test should test the unit you are dealing with and not other units (that’s what integration, regression, load, or acceptance tests are for).
The method you are testing has the job to issue SQL queries and receive the results. Therefore, its unit test should exercise that behaviour and verify it. Whether that means using a real database, an in-memory test database or a completely fake mock database that does nothing but respond with canned results depends on which is easiest to do. The important point is that you verify “a SELECT ID FROM PUPILS WHERE… goes out, and the results are stored or returned”.
3