Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test an SQL query?

I have a class DBHandler which takes a query, runs it through the SQL server, checks for errors and returns the result. How can I unit test this class?

Edit: I'll try to be more precise: DBHandler is in charge of passing the query to the server. In order to test that it actually does that, throws the correct exceptions, etc., I want to connect it to a mock DB which I will populate. My question is - how to do that? How can I create a mock "server" that handles calls?

like image 513
Amir Rachum Avatar asked Nov 24 '10 10:11

Amir Rachum


1 Answers

Just pass a SQL query, and compare the returned result to expected result. Simple. JUnit is a unit test framework, you can utilise that.

For sophisticated database unit testing, look at DBUnit.

like image 102
Adeel Ansari Avatar answered Oct 12 '22 23:10

Adeel Ansari