Skip to content
This repository was archived by the owner on Oct 17, 2023. It is now read-only.

Spring Ws

Lukas Krecan edited this page Nov 16, 2013 · 1 revision

Spring WS extension

Introduction

Extending Spring WS tests is straightforward. Just add

<dependency>
	<groupId>net.javacrumbs</groupId>
	<artifactId>smock-springws</artifactId>
	<version>0.5</version>
	<scope>test</scope>
</dependency>

Extended Client Test

//import Smock classes
import static net.javacrumbs.smock.springws.client.SmockClient.createServer;
import static net.javacrumbs.smock.springws.client.SmockClient.withMessage;
...

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:client-config.xml"})
public class CalcInterceptorTest {
    @Autowired
    private Calc calc;
    
    private MockWebServiceServer mockServer;
    
    //inject mock control
    @Autowired
    private WebServiceTemplate webServiceTemplate;
    
    @Before
    public void setUpMocks() throws Exception {
        //hook up into the Spring WS and set up interceptors if needed
        mockServer = createServer(webServiceTemplate, new EndpointInterceptor[]{new PayloadLoggingInterceptor()});
    }
    
    @After
    public void verify()
    {
    	mockServer.verify();
    }

    @Test
    public void testSimple()
    {
    	mockServer.expect(anything()).andRespond(withMessage("response1.xml"));

    	int result = calc.plus(1, 2);
	assertEquals(3, result);
    }
}

Extended Server Test

import static net.javacrumbs.smock.springws.server.SmockServer.*;
import static org.springframework.ws.test.server.ResponseMatchers.*;

...

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring-ws-servlet.xml"})
public class EndpointTest {
	private MockWebServiceClient client;
	
	@Autowired
	public void setApplicationContex(ApplicationContext applicationContext)
	{
		client = createClient(applicationContext, null);
	}
	
	@Test
	public void testSimple() throws Exception {
		client.sendRequest(withMessage("request1.xml")).andExpect(noFault());
	}

}

Base class for standard Spring WS tests

If you do not want to use Smock extension, but you would like to have a base class for your tests to extend from. In such you can extend AbstractWebServiceServerTest or AbstractWebServiceClientTest.

Base class for extended tests

If you do not mind using extended features of Smock you can extend AbstractSmockClientTest or AbstractSmockServerTest respectively. In such case your test will be simpler. Specifically, you do not have to care about MockWebServiceServer instance.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:client-config.xml"})
public class CalcSimpleTest extends AbstractSmockClientTest {
    @Autowired
    private Calc calc;
    
    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext)
    {
    	createServer(applicationContext);
    }
   
    @After
    public void verify()
    {
    	super.verify();
    }

    @Test
    public void testSimple()
    {
	expect(anything()).andRespond(withMessage("response1.xml"));
	int result = calc.plus(1, 2);
	assertEquals(3, result);
    }
}

Clone this wiki locally