That looks good Chris -- albeit there are some exceptions that will have to be
wrapped. I believe these are java.lang.NoSuchFieldException;
java.lang.IllegalAccessException and potentially some others.
-- here's a snippet of something I've done in the past though not with
control implementations:
String varName = "_isValidUserMap";
Field userValidityMap = null;
try {
userValidityMap = getField(beanMockUnderTest.getClass(), varName);
userValidityMap.setAccessible(true);
Map<String, Boolean> userMapUnderTest = (Map<String, Boolean>)
userValidityMap.get(beanMockUnderTest);
for (String user : userMapUnderTest.keySet()) {
assertTrue("we expect all users to be valid",
userMapUnderTest.get(user));
}
} catch (NoSuchFieldException e) {
fail("we expect _isValidUserMap to be found.");
} catch (IllegalAccessException e) {
fail("we expect to be able to access _isValidUserMap field");
}
as far as I can tell -- your code looks fine. Also, there is a nice Mock
objects tutorial on dev2dev right now that can help on cutting down on code
swell @
http://dev2dev.bea.com/pub/a/2007/06/mock-shortcomings.html
it doesn't go over reflection though, but that's ok.
Good luck!
Jason.
Jason Knox <[EMAIL PROTECTED]> wrote:
-----Original Message-----
From: Christopher Snow [mailto:[EMAIL PROTECTED]
Sent: Thursday, August 30, 2007 8:51 PM
To: Beehive Users
Subject: Re: testing controls
Jason,
I don't know that much about reflection (time to learn?) - will
something like this work?:
// the class we want to test
IControlA controlA = new ControlAImpl();
// controlA's reference to IControlB that we want to mock
IControlB mockControlB = new MockControlBImpl();
Field field = controlA.getClass().getDeclaredField("controlB");
field.setAccessible(true);
field.set(controlA, mockControlB);
Many thanks,
Chris
On 30 Aug 2007, at 16:47, jason knox wrote:
> Hi Chris --
> One option that I've taken is to test the implementation of
> ControlA directly. With this approach one can either use A.) a
> protected setter and set the ControlB mock in the testclass
> (provided they share the same directory structure) or B.) simply
> set the control using reflection either explicitly or with a
> private setter. Although I've most recently used option B.) our
> team has come to the conclusion that option A.) is sufficient.
>
>
> Hope this helps,
> Jason.
>
> -----Original Message-----
> From: Christopher Snow [mailto:[EMAIL PROTECTED]
> Sent: Thursday, August 30, 2007 6:46 AM
> To: [email protected]
> Subject: testing controls
>
> I have a control (ControlA) that has a reference to another
> control (ControlB).
> I would like to test ControlA using a mock of ControlB. I have
> created a
> setting to set ControlA's reference to ControlB from the test
> case. However to
> do this, I need to put the setter in the business interface. Is
> there a better
> way?
>
> Thanks in advance ...
>
>
> @ControlImplementation
> public class ControlAImpl ... {
>
> @Control
> private ControlB controlB;
>
> public void setControlB( ControlB control ) {
> controlB = control
> }
>
> // business logic
> }
>
> @ControlInterface
> public interface ControlA ...{
> // bad idea? non business method in business interface
> public void setControlB( ControlB control );
> }
>
>
> public class ControlAImplTest extends ControlTestCase {
>
> @Control
> private ControlA controlA;
>
> public void testX() {
> ControlB mockControlB = // create mock of ControlB
> controlA.setControlB( mockControlB )
>
> // test business logic here
> }
> }
>
> ----------------------------------------------------------------
> This message was sent using IMP, the Internet Messaging Program.
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
>
>
>
> ---------------------------------
> Choose the right car based on your needs. Check out Yahoo! Autos
> new Car Finder tool.
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
>
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Notice: This email message, together with any attachments, may contain
information of BEA Systems, Inc., its subsidiaries and affiliated entities,
that may be confidential, proprietary, copyrighted and/or legally privileged,
and is intended solely for the use of the individual or entity named in this
message. If you are not the intended recipient, and have received this message
in error, please immediately return this by email and then delete it.
---------------------------------
Pinpoint customers who are looking for what you sell.