atom feed2 messages in org.codehaus.jmock.userRE: [jmock-user] Mocking with beans p...
FromSent OnAttachments
Mikael PettersonNov 21, 2007 12:27 am 
Jörg SchaibleNov 21, 2007 10:54 am 
Subject:RE: [jmock-user] Mocking with beans part 2
From:Jörg Schaible (Joer@Elsag-Solutions.com)
Date:Nov 21, 2007 10:54:45 am
List:org.codehaus.jmock.user

Hi Mike,

________________________________

From: Mikael Petterson Sent: Wednesday, November 21, 2007 9:28 AM Subject: [jmock-user] Mocking with beans part 2

Hi,

I really appreciate you taking the time to help me out J This is what is bothering me: I call this method in spCallHandler:

private AnnoData getAnno(, String serviceProvider, Integer id, String lang) { logger.debug("getAnno()"); AnnoData data = null; try { data = getAnnouncementServiceHome().create() .getAnnouncementWithOffset(announcement.getName(), serviceProvider, id, lang); } catch (RemoteException e1) {

} return data; }

I don't really want to mock further (thanks for showing me how to handle
Context. Interesting!) since then it does not become a unit test. It will span
over entity beans too.

Problem (code above): This method returns AnnoData that contains a collection of UiObjectReadOnly
that are entity beans. And when I use the AnnoData objbect later in my code it must contain
UiObjectReadOnly (beans). So the problem is to make getAnno return faked AnnoData with faked
UiObjectReadOnly objects. Can I create them in my test code? If I

do:

public static class UiObjectReadOnly implements UiObjectReadOnlyHome{ //impl. Of getters and setters. }

Is it possible? Or am I into deep water?

________________________________

Well, in your example above, you must have put a JMock proxy for your
AnnouncementServiceHome interface into the proxied InitialContext:

Mock announcementServiceHome = mock(AnnouncementServiceHome.class); initialContextMock.stubs().method("lookup").with("<whatever you use as lookup
string>").will(returnValue(announcementServiceHome.proxy())); announcementServiceHome.stubs().method("create").will(returnValue(XXX));

XXX depends on what you wanna test. It might be another JMock proxy object or a
real AnnouncementService implementation. As simple guide: Do not mock data
objects, only services. In this example you already provided with XXX an object
that contains exactly the data you like. Or (if it is a service as the class
name implies) you might use another mock, that return the test data. You have to
decide, whether the call to getAnnouncementWithOffset(...) is easier to
implement with the help of a new mock proxy that returns prepared data (as
already done above with the home interface) or if you can easily setup a real
AnnouncementService instance. Keep in mind, that you normally want to test the
code of the current class and probably not the one of AnnouncementService.

- Jörg