You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@beehive.apache.org by Christopher Snow <sn...@coralms.com> on 2007/09/04 09:19:55 UTC
Re: FW: testing controls
Jason,
Thanks for your help!
My solution is below.
Chris
@ControlImplementation
public class MyControllerImpl implements MyController, Serializable {
private static final long serialVersionUID = 1L;
@Control
private ExampleWebServiceClientControl exampleWebServiceClientControl;
// business method delegating to exampleWebServiceClientControl
public void confirm() {
exampleWebServiceClientControl.hello();
}
}
public class MyControllerImplTest extends TestCase {
// the mock environment
Mockery mockery = new Mockery();
public void test() throws ControlTestException {
// The control we want to test
MyControllerImpl control = new MyControllerImpl();
// a delegate control that is used by the control we are testing
final ExampleWebServiceClientControl mockWScontrol =
mockery.mock(ExampleWebServiceClientControl.class);
injectDependencies(control, mockWScontrol);
mockery.checking( new Expectations () {{
// expect mockWS.hello() to be called once
one ( mockWScontrol ).hello();
}});
// run the business method
control.confirm();
// verify our expectations
mockery.assertIsSatisfied();
}
/**
* Sets the private control variables of the object under test to
* simulate the variables being injected
*/
private void injectDependencies(MyControllerImpl controller, final
ExampleWebServiceClientControl mockWScontrol) {
Field field = null;
try {
field =
controller.getClass().getDeclaredField("exampleWebServiceClientControl");
field.setAccessible(true);
field.set(controller, mockWScontrol);
} catch (Exception e) {
e.printStackTrace();
}
}
}
----------------------------------------------------------------
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.