Powermockito:模拟构造函数

时间:2016-12-10 07:49:54

标签: java junit mockito webservice-client powermockito

我有一个Service类(MyService),它使用构造函数实例化一个WebserviceClient类(WSService_Service)。

我尝试使用

进行模拟
PowerMockito.whenNew(WSService_Service.class).withNoArguments().thenReturn(wSService);

但是它不会工作,它仍然会通过调用WebserviceClient的超级方法的WSService_Service类构造函数。

如何在下面的methodToTest()方法中模拟构造函数?

 @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..
}

0 个答案:

没有答案