I would like to use Mock Objects (www.mockobject.com) to mock test my web applications? It seems very handy. Only problem is that it's a little bit difficult to an practical overview how to actually do it...
Can anyone clearify me or can anyone point me any tutorials?
- Mock Objects by Ian Mitchell on September 16 2003 11:13 EDT
- Mock Objects by Rod Johnson on September 18 2003 07:31 EDT
- Compatibility by V.C. Madhu on September 19 2006 06:04 EDT
You've sort of hit the nail on the head by asking that question, because mock objects are basically tools (instrumented test stubs). They are not a process to be followed.
You're probably best off using a technique like test-driven development and then identifying the specific subsystems and/or components which would benefit from mocking-up.
I think mock objects are a Very Good Thing. They can enable you to run your entire test suite very quickly and test business objects outside a container (very important). I've used them very successfully in practice.
I don't know of any online tutorials, although I'm considering writing one for my next book. (Guess you don't want to wait until March though :-)
However I would offer the following advice to make it simpler to write mock object tests:
- Program to interfaces rather than classes. This is good OO practice anyway and it allows the use of dynamic mock objects like EasyMock.
- Put business logic in POJOs without dependence on EJB or Servlet APIs. This will make your business logic easy to test outside a container.
- Don't use entity beans, which are a testing nightmare. Don't use EJB at all without good reason.
- Avoid certain non-testable idioms such as the Singleton.
- Consider using a framework such as Spring that's designed to make application code easy to test. For example it uses Inversion of Control so that business objects don't depend on the container or Spring APIs, and can thus be tested in plain JUnit test cases.
Remember that unit tests should test each class in isolation. So you want to mock the collaborators as well as framework classes.
I think one of Vincent Massol's books may talk about mock objects.
I totally agree with you Rod.
> I think one of Vincent Massol's books may talk about mock objects.
Yes, "JUnit in Action" and it is available now in the form of PDF documents under Manning's Early Access Program. ( http://www.manning.com/massol/index.html )
This book is an excellent introduction to unit testing. Manning has a number of recently published or announced books that are worth checking out.
For German speakers I recommend the JUnit book of Frank Westphal which will be published early next year.
He has a lot of chapters free for reading on his site: http://www.frankwestphal.de/TestgetriebeneEntwicklungmitJUnitundFIT.html
I am using Mock Objects - core 0.09 version. I would like to know whether it is compatible with J2EE 1.4 and JDK 1.4. With Regards Madhuv