Spring does also provide facilities for mocking web service servers as well as requests from clients. The chapter 6.3 in the Spring WS manual shows how to do mocking.
The Spring WS mocking facility changes the behaviour of the Web Service Template, so you can call that method in the super-class - that method would then call the Spring Mock Service Server.
Here is a sample unit test with the Spring mock service server:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-ws.xml"})
public class SetStatusFromSrsTemplateTest {
@Autowired
private WebServiceTemplate wsTemplate;
@Before
public void setUp() throws Exception {
mockServer = MockWebServiceServer.createServer(wsTemplate);
}
@Test
public void testCall() {
SomeWebServiceRequest sampleRequest = new SomeWebServiceRequest();
// add properties to the sampleRequest...
Source expectedPayload = new ResourceSource(new ClassPathResource("exampleRequest.xml"));
Source expectedResponse = new ResourceSource(new ClassPathResource("exampleResponse.xml"));
mockServer.expect(payload(expectedPayload)).andRespond(withPayload(expectedResponse));
instance.callDownStream(sampleRequest);
mockServer.verify();
}
}
The above example will make the mock service server expect exactly one request with the given payload and (if the payload received matches the expected payload) respond with the given response payload.
However, if you only want to verify that the method in the super-class is really called during the test and if you're not interested in the message exchange following that call, you should use Mockito.
If you'd like to use Mockito, I'd suggest the spy (see also Kamlesh's answer). E.g.
// Decorates this with the spy.
MyClass mySpy = spy(this);
// Change behaviour of callWebservice method to return specific response
doReturn(mockResponse).when(mySpy).callWebservice(any(SomeWebServiceRequest.class));
// invoke the method to be tested.
instance.callDownstream(request);
// verify that callWebService has been called
verify(mySpy, times(1)).callWebService(any(SomeWebServiceRequest.class));