1 Reply Latest reply on Feb 15, 2011 9:32 PM by Keith Babo

    Making Unit Testing easier

    Tom Fennelly Master

      Trying to make unit testing for SwitchYard a bit easier... reduce the amount of code etc.

       

      1st iteration is available on the SWITCHYARD-36 branches in my core and components forks.

       

      I created 2 new JUnit TestCase class named SwitchYardTestCase and SwitchYardCDITestCase (just extends SwitchYardTestCase and fires up the weld container). 

       

      The job of SwitchYardTestCase is to:

       

      1. Create and hold a SwitchYard ServiceDomain instance for the tests.
      2. Provides methods for registering services in the ServiceDomain.
      3. Provides a method for creating a new Invoker class (also a new class) instance for invoking service operations on services in the ServiceDomain.

       

      Lets look at an example of how we write tests today (CDI bean service example):

       

      ServiceDomain domain = ServiceDomains.getDomain();
      
      // Consume the OM model...
      MockHandler responseConsumer = new MockHandler();
      org.switchyard.Service service = domain.getService(new QName("BasicOrderManagementService"));
      Exchange exchange = domain.createExchange(service, new BaseExchangeContract(new InOutOperation("createOrder")), responseConsumer);
      
      Message inMessage = exchange.createMessage();
      inMessage.setContent(new OrderRequest("D123", "ABCD"));
      
      exchange.send(inMessage);
      
      // wait, since this is async
      responseConsumer.waitForOKMessage();
      
      OrderResponse response = (OrderResponse) responseConsumer.getMessages().poll().getMessage().getContent();
      Assert.assertEquals("D123", response.orderId);
      

       

       

      With the current work I have done, the above test would be coded as follows (with the junit test class extending SwitchYardCDITestCase):

       

      Message responseMsg = newInvoker("BasicOrderManagementService.createOrder").
                                  sendInOut(new OrderRequest("D123", "ABCD"));
      
      OrderResponse response = (OrderResponse) responseMsg.getContent();
      
      Assert.assertEquals("D123", response.orderId);
      

       

       

      Note that we've removed the burden of setting up a lot of the Exchange plumbing code.

       

      The Invoker class has a fluent API and also provides some other methods not shown in the above example, such as a method for setting the timeout for an IN_OUT send, methods for setting the input, output and fault types for the ExchangeContract etc.

        • 1. Making Unit Testing easier
          Keith Babo Master

          This is a huge improvement.  A few follow-up items:

           

          o It would be cool if there was an easy way to specify and register transforms.

          o Invoker looks dead simple.  Would be nice to have a Provider mockup that did the same.  MockHandler was kinda like this, but I think we can do something a bit spicier like point the Provider at an interface, specify expected and returned messages, etc.

          o I don't have a lot of experience with the Mock object and XML frameworks out there, but it would be great to see some examples of how we can integrate with them.  JSON would be another interesting mock format.