JMock expectations oneOf VS one difference Is any difference in using one() or oneOf() in JMock? In cheat sheet mentioned before there is also example. Appendix A. jMock2 Cheat Sheet Introduction We use jMock2 as our mock object We’re using JUnit (we assume you’re familiar with it); jMock also. jMock 1 Documentation Stubs, Expectations and the Dispatch of Mocked Methods in jMock 1 3; Mocking Classes with jMock 1 and CGLIB 4 Cheat Sheet .

Author: Dilrajas Balabar
Country: Swaziland
Language: English (Spanish)
Genre: Science
Published (Last): 13 February 2010
Pages: 440
PDF File Size: 3.48 Mb
ePub File Size: 17.43 Mb
ISBN: 664-2-12417-321-7
Downloads: 85439
Price: Free* [*Free Regsitration Required]
Uploader: Kazrazshura

A Publisher sends messages to zero or more Subscribers. We want to test the Publisher, which involves testing its interactions with its Subscribers. We will test that a Publisher sends a message to a single registered Subscriber.

To test interactions between the Publisher and the Subscriber we will use a mock Subscriber object. First we must import the jMock classes, define our test fixture class and create a “Mockery” that represents the context in which the Publisher exists. The context mocks out the objects that the Publisher collaborates with in this case a Subscriber and checks that they are used correctly during the test.


This is a JUnit 3 test case but apart from the test case class the code will be the same when using any test framework for which jMock 2 does not have an integration layer. In older versions of jMock and JUnit 4 you can use the JMock test runner, which is less flexible than the Rules mechanism shown above.

We first set up the context in which our test will execute.

jMock 2 Cheat Sheet

We create a Publisher to test. We create a mock Chext that should receive the message. We then register the Subscriber with the Publisher.

Finally we create jomck message object to publish. Next we define expectations 1 on the mock Subscriber that specify the methods that we expect to be called upon it during the test run.

jMock – Getting Started

We expect the receive method to be called once with a single argument, the message that will be sent. After the code under test has finished our test must verify that the mock Subscriber was called as expected.


If the expected calls were not made, the test will fail. The MockObjectTestCase does this automatically. You don’t have to explicitly verify the mock objects in your tests.

The JMock test runner does this automatically. The jMock library is explored in more depth in other Cookbook recipes 2.

The Subscriber interface looks like this: JUnit 3 JUnit 4 Other. Software jMock 2 Java 1.