We're using MyBatis (3.0.5) as our or-mapping tool (and I don't have any say-so over that!).
I've created a Response
object that, through MyBatis, mapped to our [responses]
database table (we use PostgreSQL).
Actually, the ormapping structure we use is as follows:
- ResponseMapper.xml - this is the XML file where the PSQL queries are defined and mapped to the ResponseMapper.java** class and its methods
- ReponseMapper.java - An interface used by MyBatis for executing the queries defined in the XML file (above)
- ResponseDAO.java - An interface used for DI purposes (we use Spring)
- ResponseDAOImpl.java - A concrete implementation of
ResponseDAO
that actually callsResponseMapper
methods; we use Spring to inject instances of this class into our application
So, to INSERT a new [responses]
record into PostgreSQL, the code looks like this from the invoking component:
@Autowired
private ResponseDAO response;
public void doStuff()
{
int action = getAction();
response.setAction(action);
response.insert();
}
This set up works beautifully for us. However I am now trying to write a set of JUnit tests for the ResponseDAOImpl
class, and I want to make sure that it is correctly executing queries to our PostgreSQL database.
As far as I can tell, there is no way to "mock" an entire database. So my only option (seemingly) is to have the test method(s) execute a query, check for success, and then roll it back regardless.
MyBatis doesn't seem to support this kind of rollback feature. I found this post off the mybatis-user
mailing list on Old Nabble, but the poster was using Guice and his/her question seemed to be more about rolling back transactions through Guice.
If MyBatis doesn't support transactions/rollbacks (does it?!?!), then it seems like my only repireve would be if the PostgreSQL-JDBC driver supports these. I guess I could then try to configure my test methods so that they run the ResponseDAO.insert()
method, and then manually try to rollback the transaction directly through the driver (sans MyBatis).
Does SO have any experience with this? Code samples? Tips? Best practices? Thanks in advance!