I'd like to write some unit tests for some code that connects to a database, runs one or more queries, and then processes the results. (Without actually using a database)
Another developer here wrote our own DataSource, Connection, Statement, PreparedStatement, and ResultSet implementation that will return the corresponding objects based on an xml configuration file. (we could use the bogus datasource and just run tests against the result sets it returns).
Are we reinventing the wheel here? Does something like this exist already for unit testing? Are there other / better ways to test jdbc code?
There are two ways to unit test methods that use JdbcTemplate. We can use an in-memory database such as the H2 database as the data source for testing. However, in real-world applications, the SQL query could have complicated relationships, and we need to create complex setup scripts to test the SQL statements.
You have several options:
Sometimes it is necessary and useful to make these tests configurable so that these tests are only executed if the database is available. This can be done with e.g. build properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With