List Info

Thread: Re: FW: testing controls




Re: FW: testing controls
country flaguser name
United Kingdom
2007-09-04 02:19:55
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("exampleWebServi
ceClientControl");
          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.


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )