Search code examples
javamockitowiremock

How to Mock a WebMethod in Mockito


I am working on a Java project where I need to write unit tests with testng for a class that has a method similar to the following:

public boolean isValidCode(String code) {
    // Some logic here
    return service.exists(new CodeInput(code));
}

The isValidCode method calls a method exists from a service interface GenericService. The exists method in GenericService is responsible for checking the existence of a code. The issue I'm facing is that the exists method is WebMethod which I don't actually want invoked.

Here's the interface for GenericService:

@WebService(name = "GenericService", targetNamespace = "http://example.com/GenericService")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
@XmlSeeAlso({
    ObjectFactory.class
})
public interface GenericService {

    @WebMethod(operationName = "GenericExists", action = "http://example.com/GenericService/GenericExists")
    @WebResult(name = "GenericExistsResponse", targetNamespace = "http://example.com/GenericService", partName = "response")
    public GenericExists exists(
        @WebParam(name = "GenericExistsRequest", targetNamespace = "http://example.com/GenericService", partName = "CodeInput")
        CodeInput codeInput);
}

Is there any way I can mock the exists method in my GenericService class to simply return true when invoked? I just want to test the "isValidCodeMethod" for my tests.

I tried using Mockito to do the following:

CodeInput codeInput = mock(CodeInput.class);
GenericService serviceMock = mock(GenericService.class);
when(genericService.exists(any(CodeInput.class))).thenReturn(true);

But I get a null pointer exception when I do this.


Solution

  • Ensure your YourClass has the isValidCode method and inject the GenericService properly.

    import org.mockito.InjectMocks;
    import org.mockito.Mock;
    import org.mockito.MockitoAnnotations;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Test;
    import static org.mockito.ArgumentMatchers.any;
    import static org.mockito.Mockito.when;
    import static org.testng.Assert.assertTrue;
    
    public class YourClassTest {
    
        @Mock
        private GenericService genericService;
    
        @InjectMocks
        private YourClass yourClass;
    
        @BeforeMethod
        public void setUp() {
            MockitoAnnotations.initMocks(this);
        }
    
        @Test
        public void testIsValidCode() {
            // Arrange
            CodeInput codeInput = new CodeInput("testCode");
            when(genericService.exists(any(CodeInput.class))).thenReturn(new GenericExists(true));
    
            // Act
            boolean result = yourClass.isValidCode("testCode");
    
            // Assert
            assertTrue(result);
        }
    }
    
    • @InjectMocks: Injects the mock GenericService into your class.
    • MockitoAnnotations.initMocks(this): Initializes the mock objects.