Tomcat and JDBC driver
i have one webapp deployed on tomcat, that uses the JDBC driver's access to a remote db. my question is: how can i开发者_如何学运维 get in the middle, like a proxy, and mock the results (without changing a single line of Java code)?
example: there is a jdbc proxy that I can use?
thanks.
You have few options:
Use an embedded database such as H2. It even has few compatibility modes with other databases, so you won't have to change your statements.
Use a mocking library like Mockito, like Sanjay suggested.
Use dbUnit for unit testing your database.
Install your remote database on your local computer, if it's feasible.
Edit: from your comment, it seems options 1 and 2 are of interest to you.
I haven't used mocking because I didn't need it yet (I prefer using the real stuff), but I don't say it isn't useful.
So, I will focus a bit on 1:
Check out H2's tutorial, specifically, the part about connecting to database using JDBC. Also, reading the quickstart guide can't hurt. You have to read to understand ;-)
For compatibility, check the link I provided earlier and use whatever mode that's suitable for the database you're using (you didn't specify which one).
Without changing/adding a single line of code? Not that I know of. Of course if you have programmed to an interface and are planning to write JUnit tests for your code, you can look into a mocking library like Mockito to do that job for you, though, the job of "creating" the data would yours to lookout for.
The question is: what are you trying to test? Do you want to verify your SQL-statements are correct? Or transaction boundaries? Or perhaps some data-processing inside your DAO-layer? Thinking of it that way may lead you to different conclusions about what needs mocking.
精彩评论