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 reaches out to an external service, and I do not have access to the endpoint in my development environment. Therefore, I’m looking for a way to mock the exists method without having the actual endpoint available.
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 a way I can use WireMock or any other mocking framework to mock the exists method without having access to the actual endpoint? Any suggestions or examples would be greatly appreciated.
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.