java - How do I create a mock object for Spring's WebServiceTemplate? -
I have a class that calls the current web service along with the valid results generated by my class web service Wrong strings are handled properly The basic call for web service looks something like this (though it is simplified).
public string call web service (last string input xml) {string result = zero; {Try StreamSource Input = New Stream Resources (New String Reader (Input xml)); String Vector output = new stringwriter (); _webServiceTemplate.sendSourceAndReceiveToResult (_serviceUri, Input, New StreamResult); Result = Output.Tratring (); } Hold (SoapFaultClientException pre) {Results = ex.getFaultStringOrReason (); } Return results; } Now I need to make some unit tests that test all success and failure conditions. It can not call the actual web service, so I was hoping that fake objects were available for the Spring-WS client side. Is anyone aware of fake items available for WebServiceTemplate or any related category? Should I try to write my own self and use my webservervisation operation interface versus website sitemap?
Michael's answer is very close, but here is an example that works.
I have already been using Mokitu for my unit tests, so I am familiar with the library. However, unlike my previous experience with Makoto, not only help to make fun of the return result, I need to do two things to test all the cases of use:
- Stream Modify the stored value in the result.
- Throw exceptions to a simple client. First of all, I realized that I can not mock WebSerstrictal with Mokito because it is a solid square (if it is necessary then you need to use EZMock). Fortunately, the web service is part of the call, sendSourceAndReceiveToResult, WebServiceOperations interface.
The following code supports the first use case where the resulting stream is returned in the result parameter:
Private WebServiceOfferation getMockWebServiceOperations (last string result field) {WebServiceOperations mockObj = Mockito.mock (WebServiceOperations.class); DoAnswer {The answer to the public object (invitation onon charge) {try {object} [args = invocation.getArguments (); StreamResult result = (StreamResult) Arg [2]; Author output = results .getWriter (); Output Write (resultXml);} catch (IOException e) {e.printStackTrace ();} return tap;}}). When (Mock Obze) .endendor.seendSourceAndReceiveToResult (any string (), any (StreamSource.class), any (StreamResult.class)); Return mock obz; }The support for the case of another use is the same, but the exception is required to be thrown. The following code creates a SoapFaultClientException that contains faultString. The Faultcode I am testing is used by the code that handles the web service request:
Private WebServiceOffers getMockWebServiceOperations {Last string error string} {WebServiceOperations mockObj = Mockito.mock (WebServiceOperations. Class); Sabapfault Soapfalt = Mokito. Mock (simplefault.class); . When (soapFault.getFaultStringOrReason ()) thenReturn (faultString); Soapbody Soapboosi = Mokito Mock (Sopbodie class); . When (soapBody.getFault ()) thenReturn (soapFault); Sago Message Soap Message = Mokty Mock (Soap Message Class); . When (soapMsg.getSoapBody ()) thenReturn (soapBody); DoThrow (new SoapFaultClientException (soapMsg)) When (mockObj) .sendSourceAndReceiveToResult (no string (), any (StreamSource.class), any (StreamResult.class)); Return mock obz; }Both of these usage cases may require more code, but they work for my purposes.
Comments
Post a Comment