GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together. Skip to content. Permalink Dismiss Join GitHub today GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together. Sign up.
|Published (Last):||4 March 2013|
|PDF File Size:||16.25 Mb|
|ePub File Size:||6.32 Mb|
|Price:||Free* [*Free Regsitration Required]|
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.
Note : this currently only works with the latest jMock release candidate 2. 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. We create a Publisher to test. We create a mock Subscriber that should receive the message. We then register the Subscriber with the Publisher. Finally we create a 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.
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.
Links: 1. Software jMock 2 Java 1.
JMock to Scalamock Cheat Sheet
A Mockery represents the context of the object under test: the objects that it communicates with. A Mockery creates mock objects and checks expectations that are set upon those mock objects. By convention the Mockery is stored in an instance variable named context. Tests written with JUnit 3 can extend MockObjectTestCase, in which case they don't need to explictly create or refer to the context. Tests written with JUnit 4 do not need to extend a specific base class but must specify that they use jMock with the RunWith attribute, create a JUnit4Mockery that reports expectation failures as JUnit 4 test failures, and store the Mockery in an instance variable.
jMock 2 Cheat Sheet
Test Fixture Class. Expectations; import org. Mockery; import org. JMock; import org. A Mockery creates mock objects and checks expectations that are set upon those mock objects. RunWith Jmock. Creating Mock Objects.
Subscribe to RSS