1

I have a Service class(MyService) that instantiates a WebserviceClient class(WSService_Service) using constructor.

I tried mocking it using

PowerMockito.whenNew(WSService_Service.class).withNoArguments().thenReturn(wSService);

But it wont work, It would still go through the WSService_Service class constructor which calls a super method of a WebserviceClient.

How do I mock the constructor in methodToTest() method below?

 @Service
    public class MyService {

    public void methodToTest(){
        WSService  wsService_Service = new WSService_Service().getWSServicePort();
        SomeObjectResponose = wsService_Service.someService(); // I want to verify if this method is called.
    }

}


import javax.xml.ws.Service;

@WebServiceClient(name = "WSService", .....//some code ommitted)
public class WSService_Service extends Service{

   static {
        URL url = null;
        try {
            url = new URL("http://url.ommited");
        } catch (MalformedURLException e) {
           //code ommitted
        }
        WSDL_LOCATION = url;
    }

    public WSService() {
        super(WSDL_LOCATION, SERVICE);
    }

    public WSService(URL wsdlLocation, WebServiceFeature ... features) {
        super(wsdlLocation, SERVICE, features);
    }

    public WSService getWSServicePort() {
        return super.getPort(WSServicePort, WSService.class);
    }

    //some codes ommited..
}
charmae
  • 1,080
  • 14
  • 38
  • 1
    You need `@RunWith` and `@PrepareForTest` annotations in your test case. Please see http://stackoverflow.com/questions/25317804/using-powermockito-whennew-is-not-getting-mocked-and-original-method-is-called – DLight Dec 10 '16 at 10:10
  • Add the code for the Test class as well along with current `@Runwith` annotations – Vasu Dec 10 '16 at 11:50
  • @gcharmae You're welcome – DLight Dec 11 '16 at 11:20

0 Answers0