Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking a MySQL server with Java

Since I'm not really proficient with databases, some details may be irrlevant, but I'll include everything:

As part of a project in my University, we're creating a website that uses JSP, servlets and uses a MySQL server as backend.

I'm in charge of setting up the tables on the DB, and creating the Java classes to interact with it. However, we can only connect to the MySQL server from inside the University, while we all (7 people) work mostly at home.

I'm creating an interface QueryHandler which has a method that takes a string (representing a query) and returns ResultSet. My question is this: How do I create a class that implements this interface which will simulate a database and allow others to use different DBHandlers and not know the difference and allow me to test different queries without connecting to the actual MySQL database?

EDIT: I'm not so sure on the differences between SQL databases, but obviously all the queries I run on MySQL should run on the mock.

like image 710
Amir Rachum Avatar asked Dec 10 '10 15:12

Amir Rachum


2 Answers

Why not just install your own MySQL database for testing? It runs on Windows, Mac and Linux, and it's not too resource heavy. I have it installed on my laptop for local testing.

like image 50
Paul Tomblin Avatar answered Sep 28 '22 07:09

Paul Tomblin


Your API appears to be flawed. You should not be returning ResultSets to clients. By doing so, you are forever forcing your clients to rely on a relational database backend. Your data access layer needs to hide all of the details of how your data is actually structured and stored.

Instead of returning a ResultSet, consider returning a List or allowing the client to supply a Stream that your data access component can write to.

This will make unit tests trivial for the clients of the API and will allow you to swap storage mechanisms at will.

like image 33
Jeff Knecht Avatar answered Sep 28 '22 06:09

Jeff Knecht