2015-02-05 34 views
0

所以基本上我正在尝试使用powermockito为适用于服务类的适配器编写一个使用web服务的Junit。Powermockito无法模拟超级调用

我有一个适配器,带有一个构造函数,inturn通过调用超类在它自己的构造函数中创建一个新的服务对象。我必须测试我的适配器。我用mockito来模拟我的适配器以及我的服务类,但我不认为模拟对象能够执行超级调用。以下是我的代码结构。我希望超类能够在通话时返回我的模拟对象。

public class CommonPoolingServiceAdp { 

    private CPSSecurity cpsServicePort; 

    public CommonPoolingServiceAdp() {  
     CommonPoolingService service= new CommonPoolingService(); 
     cpsServicePort=service.getCommonPoolingServicePort(); 
    } 

    public SercurityDataResponse getBroadcastElements(broadcastReqObj) 
    { 
     SercurityDataResponse=null; 
     response=cpsServicePort.getBroadcastElements(broadcaseRequestObj); 
    } 
} 

public class CommonPoolingService extends Service { 

    { 
    static 
    { 
     //few mandatory initializations 
    } 

    public CommonPoolingService() 
    { 
     super(WSDL_Location,QName); 
    } 

    public CSPSecurity getCommonPoolingServicePort() { 
     return super.getPort(QName); 
    } 

    } 
} 
+0

你可以发布你当前的测试代码。所以你想调用'super.getPort(QName)'来返回一个模拟'CSPSecurity'? – clD 2015-02-09 12:16:24

回答

0

请多分享一下您的代码。顺便说一句,这是你如何嘲笑超级方法:

public class SuperClass { 

public void method() { 
     methodA(); // I don't want to run this! 
} 
} 
public class MyClass extends SuperClass{ 
public void method(){ 
    super.method() 
    methodB(); // I only want to test this! 
} 
} 

@Test 
public void testMethod() { 
MyClass spy = Mockito.spy(new MyClass()); 

// Prevent/stub logic in super.method() 
Mockito.doNothing().when((SuperClass)spy).methodA(); 

// When 
spy.method(); 

// Then 
verify(spy).methodB(); 
} 

希望,这将有所帮助。

相关问题