Simplify your WS tests

Update: Spring WS 2.0 contains support for both server side and client side testing. If you are using Spring WS 2, use that. If you are Axis2, CXF or Metro user, take a look at Smock library.

Do you need to test you web-service client layer? Do you wan to run your end-to-end test without having to set-up a mock server? Do you want to simplify your server side tests? Spring WS Test is here to help you.

Spring WS Test project simplifies functional testing of Spring WS client applications. It allows you to write functional tests that can run in one JVM, that can use JUnit or TestNG, that are simple to set-up and configure. Yet this type of tests is quite powerful and you can test most of your configuration and component integration.

This project can be used mainly in the following scenarios:

  • Tests of Spring configuration. Unit tests are nice and useful but we also need to test the configuration. Spring WS Test gives you the possibility to test most of the configuration without having to depend on any external systems running.
  • Test Driven Development. You can define an application in terms of requests sent and reactions on responses received. Spring WS Test makes it much easier.
  • ...

    If you are looking for server side testing, please go here. Following sections are about WS client tests.

How to start?

  1. Import Maven dependency
    <dependency>
            <groupId>net.javacrumbs</groupId>
            <artifactId>spring-ws-test</artifactId>
            <version>0.22</version>
    </dependency>
  2. Write your test
    @RunWith(SpringJUnit4ClassRunner.class)
    //load your standard Spring configuration 
    @ContextConfiguration(locations={"classpath:spring/applicationContext.xml"})
    //Add the listener
    @TestExecutionListeners({WsMockControlTestExecutionListener.class, DependencyInjectionTestExecutionListener.class})
    public class AirlineWsClient4Test {
    
            //inject the class under the test
            @Autowired
            private AirlineWsClient client;
            
            //inject mock control
            @Autowired
            private WsMockControl mockControl;
            
            @Test
            public void testCall()
            {
                    
                    //teach mock what to do 
                    mockControl.expectRequest("expected-request.xml")
                                            .returnResponse("response-to-be-returned.xml");
    
                    //call WS client code
                    long ticketId = client.bookFlight(input,params,here);
                    assertEquals(123456L, ticketId);
                    
                    //verify all the calls
                    mockControl.verify();
            }
            
    }
    
  3. And you are done. The library will verify that your Spring configuration is correct, that the request generated by your code corresponds to what you expect and the framework will return the response specified in the "response-to-be-returned.xml" file. Everything in memory, without any HTTP connection so it's reasonably fast.

    If you need more functional test style configuration, please read functional test reference.

    Note: I really want to hear your feedback. The project is in very early stage so you have unique opportunity to drive its development. If you find a bug, please report it in project JIRA. If you encounter any issue, do not hesitate and contact me on lukas at krecan dot net. If you like it, please let me now on my blog