Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Mockito to test SOAP messages?

I'm fairly new to Mockito, and figured I would try to use it to test a SOAP Handler. However, this is turning out to be a much more painful than I would have expected/desired.

I'm looking to validate that my handler is able to extract the messageID in the header of a SOAPMessage. However, from the handler, the only way to get to the header is via the context/message/part/envelope/header. Using Mockito my solution was to mock my SOAPMessage, meant creating each individual object, and stubbing the method.

I can only imagine that there is an easier/cleaner way of accomplishing this:

@RunWith(MockitoJUnitRunner.class)
public class UUIDHandlerTest {

    @Mock private SOAPMessage message;
    @Mock private SOAPEnvelope envelope;
    @Mock private SOAPHeader header;
    @Mock private SOAPPart part;

    @Mock
    private SOAPMessageContext context;

    @Before
    public void setup() throws SOAPException{
        when( context.getMessage()).thenReturn(message);
        when( message.getSOAPPart()).thenReturn(part);
        when( part.getEnvelope()).thenReturn(envelope);
        when( envelope.getHeader()).thenReturn(header);
    }


    @Test
    public void testHandleInboundMessage() {
        when( context.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)).thenReturn(false);

        when(header.getElementsByTagName(anyString())).thenAnswer(new Answer<NodeList>() {
            /* (non-Javadoc)
             * @see org.mockito.stubbing.Answer#answer(org.mockito.invocation.InvocationOnMock)
             */
            @Override
            public NodeList answer(InvocationOnMock invocation) throws Throwable {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(new ByteArrayInputStream(new String("<wsa:MessageID>messageId</wsa:MessageID>").getBytes()));

                // TODO Auto-generated method stub
                return doc.getElementsByTagName("wsa:MessageID");
            }

        });


        // call the test class
        new UUIDHandler().handleMessage(context);

        // check the MDC value
        assertEquals("messageId", MDC.get(LoggerConstants.DC_PROPERTY_MESSAGE_ID));
    }
}

Like I said, it works, but it looks like a very ugly/heavy weight solution.

Is there anyway to do this easier/cleaner?

Thanks!

Eric

like image 514
Eric B. Avatar asked Feb 15 '12 21:02

Eric B.


People also ask

How do you unit test a SOAP web service?

Unit Test Web Service Client Let us open the command console, go to the C:\MVN\countryService directory and execute the following mvn command. Maven will start building and testing the project.

What is Mockito stub method?

A stub is a fake class that comes with preprogrammed return values. It's injected into the class under test to give you absolute control over what's being tested as input. A typical stub is a database connection that allows you to mimic any scenario without having a real database.

What Mockito mock does?

The Mockito. mock() method allows us to create a mock object of a class or an interface. We can then use the mock to stub return values for its methods and verify if they were called. We don't need to do anything else to this method before we can use it.

How do I create a test case in Mockito?

The following steps are used to create an example of testing using Mockito with JUnit. Step 1: Create an interface named ToDoService that contains an unimplemented method. Step 2: Now, create an implementation class named ToDoBusiness for ToDoService interface.


1 Answers

    SOAPMessageContext context = 
            mock(SOAPMessageContext.class, RETURNS_DEEP_STUBS);
    when(context.getMessage().getSOAPPart().getEnvelope().
            getHeader().getElementsByTagName(anyString())).
            then(...);

Please also pay attention to the notes on using deep stubs in the mockito documentation. http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#RETURNS_DEEP_STUBS

Annotation style:

    @Mock(answer = Answers.RETURNS_DEEP_STUBS) SOAPMessageContext context;
like image 68
Thomas Avatar answered Nov 15 '22 10:11

Thomas