You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

Introduction


The intent of this blog entry is to track what I learn while working through an example using JUnit 4 and JMock 2.5.1. This entry is a work in progress and will be updated as I go along. By the end of this, I think you will agree that jMock is awesome and can provide you with a very good tool to mock-up something that might be heavyweight and unit testing in general is absolutely critical to production software.

JUnit


Let's get the introductions out of the way for those who don't know about JUnit. Put simply, JUnit is an open source testing framework used to test source code. It is critical for any production application to include a thorough set of unit tests so that errors can be detected immediately, even before checking new code in. Every class should have a unit test to verify that it behaves as the author expected and to ensure this is the case, a unit test should be written along with the new class so that tests can be set up immediately. Any subsequent changes to the class can be verified by re-running its test before the changes are commited to the repository. If an untested scenario is discovered, then it can be added to the unit test so that the unit test evolves with the class so that future changes that cause the same problem can be detected immediately and fixed.

jMock


jMock is a library that supports test-driven development of Java code with mock objects. Mock objects are useful because they can help you test the interaction between the objects in your program. Here are some bullet points from their website:

The jMock library:

  • makes it quick and easy to define mock objects, so you don't break the rhythm of programming.
  • lets you precisely specify the interactions between your objects, reducing the brittleness of your tests.
  • works well with autocompletion and refactoring of your IDE
  • plugs into your favorite test framework
  • is easy to extend

Now, imagine you need an object that requires an LDAP (lightweight directory access protocol) server to construct. Now, you could start an LDAP server, give it some data, run your test and then tear it down, but that's a lot of work and more closely resembles integration testing. However, a faster alternative would be to use jMock to mock up an object that returns the data you need. Mocking up objects is only the tip of the iceberg. You can also set expectations of objects (e.g. this method is only called once), set the order methods should be called in, test multi-threaded code, etc.

Example


Problems Encountered


  1. I get a java.lang.SecurityException when trying to run my unit test.
    • I saw this exception when I had two different versions of JUnit 4. One came with the Eclipse platform and the other was a plugin from a library, Tupelo, that had its own version of JUnit 4. Both contained a class JUnit4ClassRunner that when both present threw a java.lang.SecurityException. To resolve this issue, I removed the JUnit4 jar file in the Eclipse plugin directory.

Links


Links to the libraries used in the above example.

  • No labels