Advanced Client Unit Testing

Direct test

In case you want to test only your code and not Spring configuration, it is possible to use Spring-free configuration.

public class AirlineWsClient3Test {
        private static final long TICKET_ID = 123456L;
        private static final Calendar DEPARTURE_TIME = Calendar.getInstance();
        private static final String FLIGHT_NUMBER = "OK1234";
        
        private AirlineWsClient client;
        private WebServiceTemplate webServiceTemplate;
        

        private Passenger[] createPassengers() {
                return new Passenger[]{new Passenger("Joe","Smith"), new Passenger("John","Doe")};
        }
        
        @Test
        public void testCall()
        {
                Passenger[] passengers = createPassengers();
                
                //create control
                WsMockControl mockControl = new WsMockControl();
                //teach mock what to do and create it
                WebServiceMessageSender mockMessageSender = mockControl
                                        .validateSchema("xsd/messages.xsd", "xsd/types.xsd")
                                        .expectRequest("mock-xml/www.csa.cz/BookFlightRequest/OK1234-request.xml")
                                        .returnResponse("mock-xml/www.csa.cz/BookFlightRequest/OK1234-response.xml")
                                        .createMock();
                webServiceTemplate.setMessageSender(mockMessageSender);
                
                long ticketId = client.bookFlight(FLIGHT_NUMBER, passengers, DEPARTURE_TIME);
                assertEquals(TICKET_ID, ticketId);
                
                mockControl.verify();
        }
        
        @Before
        public void setUp() throws Exception
        {
                client = new AirlineWsClient();
        
                
                webServiceTemplate = new WebServiceTemplate();
                Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
                marshaller.setContextPath("net.javacrumbs.airline.aggregator.jaxb");
                marshaller.afterPropertiesSet();
                webServiceTemplate.setMarshaller(marshaller);
                webServiceTemplate.setUnmarshaller(marshaller);
                client.setTemplate(webServiceTemplate);
        }
        
}