Making Unit Testing easier
tfennelly Feb 15, 2011 8:55 AMTrying 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:
- Create and hold a SwitchYard ServiceDomain instance for the tests.
- Provides methods for registering services in the ServiceDomain.
- 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.