Unit testing - Mock objects for SqlSession and SqlSession factory

1,804 views
Skip to first unread message

Nicolai Willems

unread,
Sep 24, 2010, 4:31:51 PM9/24/10
to mybatis-user
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

Larry Meadors

unread,
Sep 24, 2010, 4:42:07 PM9/24/10
to mybati...@googlegroups.com
I have been using mockito to mock these. It's the bomb.

Larry

Nicolai Willems

unread,
Sep 24, 2010, 5:00:27 PM9/24/10
to mybati...@googlegroups.com
On Fri, Sep 24, 2010 at 10:42 PM, Larry Meadors <larry....@gmail.com> wrote:
I have been using mockito to mock these. It's the bomb.

Mockito looks very nice. How would you do in the case described below?
And what are you testing when using mockito?(not asking for a how-to, 
maybee just a stub of test-code)

/Nicolai

Larry Meadors

unread,
Sep 24, 2010, 5:10:04 PM9/24/10
to mybati...@googlegroups.com
I use mockito to mock the database interaction so I can test my other classes.

Here's the pidgin code (gmail sucks as an IDE):

SqlSessionFactory ssf = mock(SqlSessionFactory.class);
SqlSession ss = mock(SqlSession.class);
SomeMapper some = mock(SomeMapper.class);

when(ssf.openSession()).thenReturn(ss));
when(ss.getMapper(SomeMapper.class)).thenReturn(some);

You could probably put together a Answer class to reply to getMapper()
with a mocked mapper, but for the most part, it'd be useless because
you need to provide the mocked behavior.

Larry

Tim

unread,
Sep 24, 2010, 5:38:10 PM9/24/10
to mybati...@googlegroups.com
That's awesome stuff Larry but bomb's are not cool.
Remember when Mac's errored out with the bomb guy?
No one thought he was cool.

And now you know. And knowing is half the battle.

Larry Meadors

unread,
Sep 24, 2010, 5:40:23 PM9/24/10
to mybati...@googlegroups.com
It's Friday. Who let Tim out of his cage? :)

Tim

unread,
Sep 24, 2010, 5:43:03 PM9/24/10
to mybati...@googlegroups.com
My engrish is the bomb (see? bad connotation)
s/bomb's/bombs/

Clinton Begin

unread,
Sep 24, 2010, 10:33:28 PM9/24/10
to mybati...@googlegroups.com
Sorry for not reading the whole post... kind of in a rush... I'll get out two points, do with them what you will:

* I don't mock them.  I have layered testing, so my mappers hit the database in a suite of tests specifically designed to test the *SQL*... this is an important part of unit testing.  Whoever said don't hit the database when testing was smoking something.  :-)  Perhaps don't do it without care, but SQL must be part of your testing scope, or your app isn't covered.

*  As for isolation of layers above the Data Access Layer, I tend to use mappers, which are easily "mockable" because they are just pojos really...  Mixed with a good DI framework like Guice, most of my application doesn't even know what MyBatis is...

But when you do need to, Larry's advice for Mockito is great.

Clinton

On Fri, Sep 24, 2010 at 2:31 PM, Nicolai Willems <niw...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages