Monday, August 18, 2008

[Arch] Mock Objects

I stumbled over this article yesterday: A neat and short description of Mock objects and a motivation how Mock objects in general and Mocking frameworks can support (unit) testing particularly with classes that have dependencies. I like this very short introduction because the concept of Mock-objects is actually not so difficult to understand but the need for Mock-frameworks is not so easy to grasp.

If the basic idea is understood the documentation of frameworks like JMock can kick in and do the rest ;-)

Addition: Thanks to the comment of reader Touku who recommended the article from Martin Fowler: Mocks Aren't Stubs.

2 comments:

Touko said...

Yes, mocks are nice and valuable tools with (unit) testing.

I personally found it good to understand the difference between mocks and stubs (valuable testing methods for about the same purpose but with a different approach). Martin Fowler has a good article on the topic: Mocks Aren't Stubs

Prof. Dr. Stefan Edlich said...

By the way, I find Mockito even easier to use as JMock and Easy Mock.

See this example:
Gehalt g = new Gehalt();
Krk kkasse = mock(Krk.class); stub(kkasse.getKVersich()).toReturn(300.0);
g.setKrk(kkasse);
// then use g

And this covers 90% of all mock need I have.

Best
Stefan Edlich