|
jDummy | |||||||||
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |
See:
Description
Class Summary | |
---|---|
JDummyTestCase | A base class for tests that use jDummy syntax. |
Mockery | Factory for all Mock objects. |
Tools for automatically creating dummy objects and values for jMock test cases.
To use jDummy, just derive your test case from JDummyTestCase
and use the methods
defined there.
When writing tests with jMock, you often find yourself writing code for dummy data: creating it, writing stubs that return the data, and expectations that the data is passed around correctly. In some cases, quite a lot of code is needed just to set-up the test.
For example, the following code is setting up stub behavior so that the user's site is the same as the line item's requisition's site. It then asserts the expectation that the remote server creates a session using the user's id. The English is a bit shorter that the jMock code:
final String DUMMY_USER_ID = "userID-1"; Mock mockUser = mock(User.class); Mock mockUnit = mock(FulfillmentUnit.class); Mock mockLineItem = mock(LineItem.class); Mock mockRequisition = mock(Requisition.class); Mock mockSite = mock(Site.class); Site dummySite = (Site) mockSite.proxy(); mockUnit.stubs().method("getLineItem").will(returnValue(mockLineItem.proxy())); mockLineItem.stubs().method("getRequisition").will(returnValue(mockRequisition.proxy())); mockRequisition.stubs().method("getSite").will(returnValue(dummySite)); mockUser.stubs().method("getSite").will(returnValue(dummySite)); mockUser.stubs().method("getUserID").will(returnValue(DUMMY_USER_ID)); mockRemoteServer.expects(once()).method("createSession").with(eq(DUMMY_USR_ID))
Using the methods inheriteed from JDummyTestCase
, the code reduces to
this:
User user = (User) mimicWithDummyValues(User.class); FulfillmentUnit unit = (FulfillmentUnit) mimicWithDummyValues(FulfillmentUnit.class); precondition(unit.getLineItem().getRequisition()) .method("getSite").will(returnValue(user.getSite())); assertBehavior(remoteServer).expects(once()).method("createSession").with(eq(user.getUserID()));
The 'mimicked' objects in the second version are just the proxies of org.jmock.Mocks. However, they have a special default stub: a dummy value generating stub.
The dummy value generating stub is similar to the DefaultResultStub: it only comes into play when a method is invoked for which no explicit stub has been provided. However, instead of user.getUserID() returning null as with the DefaultResultStub, the dummy value generating stub will return "userID": a much more useful value! If you create a second user2 = mimicWithDummyValues(User.class), however, user2.getUserID() will return "userID-2".
This means you can eliminate all code that is just creating or 'wiring up' dummy values.
In most situations, the following three methods used above are all you need:
proxy = mimicWithDummyValues(type) | creates a Mock with a dummy-value-generating default stub and returns its proxy |
assertBehavior(proxy) | returns the Mock for the proxy. It is a synonym for mockForProxy(proxy) |
precondition(proxy) | short-hand for mockForProxy(proxy).stubs() |
|
jDummy | |||||||||
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |