Hammock - Mock Objects for Java ME

Introduction

In the last decade, unit-testing with the xUnit family of test frameworks has become popular; in particular
Test-Driven Development (TDD)
has been embraced by companies and, even more importantly, by programmers as a means to improve the quality of code.
For J2ME there are several derivatives of JUnit (for example, JMUnit, J2MEUnit and MoMEUnit).
The goal of unit testing is that each class should be tested in isolation from other classes. However, in practice, this can be difficult since classes need
to collaborate to do anything useful. A popular method to work around this dependency problem is to use
mock objects.
A mock object stands in for a real object that a class under test collaborates with. The test code specifies
how a mock object
should respond when a class
under test calls methods on the mock object. By using a mock object, rather than a real object, we can simulate
scenarios that may be difficult to configure for a real object or test code where a collaborating class hasn't yet been
written yet. The ability to write tests before all collaboratoring classes have been written works well with TDD
since it enables tests to be written before the code to be tested. Additionally, testing with mock objects encourages
"programming to an interface" rather than to an implementation.
For more information
visit the mock objects website or Wikipedia.

Hammock

Mock objects can be used effectively when

The real object is difficult to configure.

The real object has behavior that is difficult to replicate (such as a network error).

The real object is a GUI component.

The real object doesn't yet exist.

Many Java ME applications can benefit from the use of mock objects for testing since typical mobile
applications have a large number of user interfaces and make use of
several network protocols (such as HTTP, SMS, bluetooth, etc). Hammock is a mock object framework for Java ME
that provides mock objects for
many collaborating classes that are used in a typical application. Mocks are provided for most of the classes in the microedition UI and I/O packages
as well as for the optional SMS, OBEX, PDA and location APIs.
Hammock includes a utility, HammockMaker, that generates the source code of classes and interfaces that need to be mocked.
HammockMaker can be run from the command line or, more usefully, as an Ant task. Since HammockMaker is an
Ant task it can be incorporated into a build, test and deploy script. A sample application,
ObjectStreams,
includes a detailed guide showing how to test effectively using Hammock.
An Eclipse plug-in for HammockMaker provides a fast way to create test doubles.
The current stable version of Hammock is version 2.1.