Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 199803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:03:19+00:00 2026-05-11T17:03:19+00:00

I have a class which calls out to an existing web service. My class

  • 0

I have a class which calls out to an existing web service. My class properly handles valid results as well as fault strings generated by the web service. The basic call to the web service looks something like this (although this is simplified).

public String callWebService(final String inputXml)
{
  String result = null;

  try
  {
    StreamSource input = new StreamSource(new StringReader(inputXml));
    StringWriter output = new StringWriter();

    _webServiceTemplate.sendSourceAndReceiveToResult(_serviceUri, input, new StreamResult(output));

    result = output.toString();
  }
  catch (SoapFaultClientException ex)
  {
    result = ex.getFaultStringOrReason();
  }

  return result;
}

Now I need to create some unit tests which test all of the success and failure conditions. It cannot call the actual web service, so I was hoping there were mock objects available for the client side of Spring-WS. Does anyone know of an mock objects available for the WebServiceTemplate or any related classes? Should I just attempt to write my own and modify my class to use the WebServiceOperations interface vs. WebServiceTemplate?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:03:19+00:00Added an answer on May 11, 2026 at 5:03 pm

    Michael’s answer is very close, but here is the example that works.

    I already use Mockito for my unit tests, so I am familiar with the library. However, unlike my previous experience with Mockito, simply mocking the return result does not help. I need to do two things to test all of the use cases:

    1. Modify the value stored in the StreamResult.
    2. Throw a SoapFaultClientException.

    First, I needed to realize that I cannot mock WebServiceTemplate with Mockito since it is a concrete class (you need to use EasyMock if this is essential). Luckily, the call to the web service, sendSourceAndReceiveToResult, is part of the WebServiceOperations interface. This required a change to my code to expect a WebServiceOperations vs a WebServiceTemplate.

    The following code supports the first use case where a result is returned in the StreamResult parameter:

    private WebServiceOperations getMockWebServiceOperations(final String resultXml)
    {
      WebServiceOperations mockObj = Mockito.mock(WebServiceOperations.class);
    
      doAnswer(new Answer()
      {
        public Object answer(InvocationOnMock invocation)
        {
          try
          {
            Object[] args = invocation.getArguments();
            StreamResult result = (StreamResult)args[2];
            Writer output = result.getWriter();
            output.write(resultXml);
          }
          catch (IOException e)
          {
            e.printStackTrace();
          }
    
          return null;
        }
      }).when(mockObj).sendSourceAndReceiveToResult(anyString(), any(StreamSource.class), any(StreamResult.class));
    
      return mockObj;
    }
    

    The support for the second use case is similar, but requires the throwing of an exception. The following code creates a SoapFaultClientException which contains the faultString. The faultCode is used by the code I am testing which handles the web service request:

    private WebServiceOperations getMockWebServiceOperations(final String faultString)
    {
      WebServiceOperations mockObj = Mockito.mock(WebServiceOperations.class);
    
      SoapFault soapFault = Mockito.mock(SoapFault.class);
      when(soapFault.getFaultStringOrReason()).thenReturn(faultString);
    
      SoapBody soapBody = Mockito.mock(SoapBody.class);
      when(soapBody.getFault()).thenReturn(soapFault);
    
      SoapMessage soapMsg = Mockito.mock(SoapMessage.class);
      when(soapMsg.getSoapBody()).thenReturn(soapBody);
    
      doThrow(new SoapFaultClientException(soapMsg)).when(mockObj).sendSourceAndReceiveToResult(anyString(), any(StreamSource.class), any(StreamResult.class));
    
      return mockObj;
    }
    

    More code may be required for both of these use cases, but they work for my purposes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class ( EAGLView ) which calls a member function of a
I'm new to learning JSPs and Servlets. I have a Java class which calls
I have class World which manages creation of object... After creation it calls afterCreation
I have a class (RInterfaceHL) that calls another class (JRIEngine) which provides native methods
I have a background worker which calls a function within a separate class. This
I have a method which calls for a classmethod of another class def get_interface_params_by_mac(self,
I have a custom MVC PHP framework that has a router class, which calls
I have some classes(call it Class A) which I would like to unit test
I have a class annotated with @Configuration (let's call it StubConfiguration ) which has
I have a simple Java class in which I invoke a call to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.