2015-08-25 31 views
0

如何在Mockito中使用super關鍵字調用方法?代碼摘錄如下給出:在mockito中使用超級關鍵字調用方法

@Override 
protected void doExecute() 
{ 
    //only show port context menu in edit mode 
    ICommandContext commandContext = getCommandContext(); 
    if(commandContext instanceof ContextMenuCommandContext) 
    { 
     ContextMenuCommandContext contextMenuContext = (ContextMenuCommandContext) commandContext; 
     if(!contextMenuContext.getRelationsContext().isAuthoringMode()) 
     { 
      return; 
     } 
    } 

    super.doExecute(); 
} 

回答

0

請試試這個, 考慮您的派生類的名字,

class BaseClass { 
    public void doExecute() {...} 
} 

public ChildClass extends BaseClass { 

    @override 
     public void doExecute(){ 
     //some codes  
     super.doExecute(); 
    } 
} 

我假設你的代碼是這樣的,

@Test 
public void testdoExecute() { 
    DerivedClassName cn= Mockito.cn(new DerivedClassName()); 

    // Prevent/stub logic in super.doExecute(); 
    Mockito.doNothing().when((BaseClassName)cn).doExecute(); 

    // When 
    cn.doExecute(); 

    // Then 
    verify(cn).load(); 
} 
+0

是什麼classname在這裏?你也沒有提到關於保存方法.. –

+0

請考慮命名你的類在你的方法存在。因爲你在上面的方法中沒有發佈問題 – VedX

+0

cn是Mockito.cn的未定義變量(新的DerivedClassName()) –