2017-07-16 147 views
1

我對自動化測試相當陌生,我一直在用mockito嘲笑一個類時遇到了一些麻煩。基本上我試圖做的是利用正在發送到方法的接口,當調用此方法(Request(Response))時,我希望mockito介入並從接口中傳遞一個方法作爲參數傳遞一個對象(callback.OnSuccess(的OBJ))。這裏是我的意思,我就開始了我的生產代碼的例子,我已經採取了原位缺口所需的一切:Mokito,嘲笑方法行爲

ServerRequest類

public void Request(ResponseInterface callback){ 
//The contents of this class isnt really important as I do not wish to use any of it 
//but in general this makes a request to the server and if the request is a success then 
Object obj = ProcessResponse(Response); 
callback.OnSuccess(obj); 
//otherwise 
Object obj = ProcessResponse(Response); 
callback.OnError(obj); 
} 

ResponseInterface

public interface ResponseInterface(){ 
void OnSuccess(Object resp); 
void OnError(Object resp); 
} 

MainActivity

public void MakeRequest(){ 
ServerRequest.Request(new ResponseInterface(){ 
@Override 
public void OnSuccess(Object objResponse){ 
//do something to show user the request was successful depending on the current activity 
} 

@Override 
public void OnError(String e){ 
//do something depending on the current activity 
}) 
} 

到目前爲止,我已經嘗試了多種方法,最好的我coul d提出的是在下面的代碼中,但是我相信你可以從帖子中看出它不起作用,但我會把它留在這裏,因爲它可能會讓某人更好地瞭解我正在嘗試做什麼。

ServerRequest mockReq = mock(ServerRequest.class); 
    Mockito.doAnswer(new Answer<Void>() { 
     @Override 
     public Void answer(InvocationOnMock invocation) throws Throwable { 
      System.out.println("Running first time"); 
      Object[] arguments = invocation.getArguments(); 
      ResponseInterface inter = (ResponseInterface) arguments[2]; 
      Object obj = "Already taken"; 
      inter.OnSuccess(obj); 
      return null; 
     } 
    }).when(mockReq).InformationRequest(ArgumentMatchers.anyMap(),anyString(),ArgumentMatchers.<ServerInterface>any()); 

任何有關此事的幫助將不勝感激,因爲我不知道我在做什麼。

+0

什麼是'RequestDataInterface'。請編輯以發佈1 /您要測試的代碼2 /測試代碼(您擁有的代碼)並刪除無關的所有內容。 – 2017-07-16 17:09:02

+0

對不起,我正在搞模擬早點忘了恢復,我想我現在已經解決了所有問題。 –

回答

0

如果有人試圖實現類似的東西,我已經想出瞭如何在通過模擬方法發送的接口中調用方法。 因此,首先我創建了以前註釋過的安裝方法(不一定需要您可以在測試之前將它放在同一個方法中)。然後我開始設置實現我的目標所需的一切,這裏是需要的設置。

//class to be mocked 
@Mock 
ServerRequest mockReq; 

@Before 
public void setup(){ 
    //get the activity instance 
    Registration reg = mActivityRule.getActivity(); 
    //make sure the actual method to be mocked does nothing, By default it should do nothing anyway but for some reason not for me 
    doNothing().when(mockReq).InformationRequest(ArgumentMatchers.anyMap(),anyString(),ArgumentMatchers.<ServerInterface>any()); 
    //set up an argument catcher 
    captor = ArgumentCaptor.forClass(ServerInterface.class); 
    //inject the mock into the activity 
    reg.serverRequest = mockReq; 
} 

接下來在我的測試方法的嘲笑方法應該被稱爲當點擊提交按鈕,在這種情況下,我驗證了模擬的方法實際上是調用,並捕獲發送給它的數據,然後我用數據我以任何方式希望。

//click the button 
onView(withId(R.id.SubmitBtn)).perform(scrollTo(), click()); 
//check to see if method was called then capture the interface 
verify(mockReq).InformationRequest(ArgumentMatchers.anyMap(),anyString(),captor.capture()); 
    //get the interface 
ServerInterface serverInter = captor.getValue(); 
Object obj = "Already taken"; 
//make use of the interface 
serverInter.OnSuccess(obj);