Hello MyBatis users
Recently I began developing with MyBatis for a common database layer
for our java apps at work
Yesterday I started doing some unit testing and found no way of doing
this nice and easy.
So I developed some Mock objects for SqlSessionFactory and SqlSession.
As for now I've
implemented openSession() and variations hereof in SqlSessionFactory
in MockSqlSession,
they all just return a MockSqlSession object. In the MockSqlSession
I've implemented getMapper(),
it uses reflection and have some simple rules:
1. You need to make MockMapperImplementations which implements the
actual interface.
2. The MockMapper need to be named Mock<MapperFileClass>, unless it
has an I(capital)
as character in name(I guess some of them have) it won't get in
there,
example ISwitchDbMapper has MockSwitchDbMapper.
3. It needs to be in the same package as your mapper-interface(This is
easily overcome by making the package in your test dir)
So for example if you have a DAO which receives a SqlSessionFactory
as parameter in the constructor,
then instead of passing in a real SqlSessionFactory you give it a
MockSqlSessionFactory, this reduces the overhead
when accessing a real database(or in my case two).
For example in your test:
@Override
protected void setUp() throws Exception {
super.setUp();
SqlSessionFactory fact = new MockSqlSessionFactory();
this.dao = new SwitchDbDao(fact);
}
I would be happy to commit these classes or I could just submit them
somewhere online accessible to all.
If you have any contributions for my implementation, please add them -
for now in email form.
I can mail them to you or you can take a look in a little while at my
blog(
http://chillems.blogspot.com) where I'm going to
post both classes.
That should be all
Regards
Nicolai Willems
Software Developer @ Zylinc.com