Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I unit test Java that calls a MySQL stored procedure?

I'm writing a simple application that will read some records and insert them in a database. I've written a stored procedure that handles the insertion logic, and plan to test that separately. Now I'd like to write a good unit test for the portion of the logic that takes a business object and passes it to the stored procedure call.

I think what I want to do is pass a mock of the database connection, then assert that the call is made with expected parameter values:

Connection dbConnection = makeMockConnection();  // how?
MyObjectWriter writer = new MyObjectWriter(dbConnection);
writer.write(someSampleObject);
// somehow assert that dbConnection called
// `sp_saveMyObject` with param values x, y, and z

However, it seems like a lot of work dig around inside java.sql.Connection, understand how it works, then mock all the results. Is there a test library that does all this for me? Am I coming at this the wrong way?

like image 875
Coderer Avatar asked Nov 14 '12 10:11

Coderer


People also ask

Should you unit test database calls?

Unit tests should never connect to a database. By definition, they should test a single unit of code each (a method) in total isolation from the rest of your system. If they don't, then they are not a unit test.


1 Answers

You could create an in-memory HSSQL database with a mock stored procedure. The mock sproc would insert a row into a table to show that it ran and what it's parameters were. Run the code under test and then look in the db to see what happened.

like image 200
AutomatedMike Avatar answered Oct 15 '22 06:10

AutomatedMike