2014-07-04 72 views
0

對象我有模擬使用構造的Mockito

Class A { 
ServiceClass serviceClass; 
public A(ServiceClass obj){ 
serviceClass = obj; 
} 
public String methodA(String i){ 
    String j = serviceClass.someMethod(i); 
return j; 
} 

} 

這裏我有一些方法在用於服務類,我已經創建的模擬對象,I M呼叫服務類方法。

ServiceClass serviceClassMock = Mockito.mock(ServiceClass.class); 
A objA = new A(serviceClassMock); 
@Test 
public void test(){ 
when(serviceClassMock.someMethod("1")).thenReturn("1"); 
String j = objA.methodA("1");// here gives me wanted be invoked but not. there where 0 interaction 

verify(serviceClassMock).someMethod("1"); 

}

+0

你能告訴我們你想要測試的方法嗎? –

+0

@DavidWallace plz check編輯 – user3060230

+0

在你的測試中是否有'verify'的地方? –

回答

1

例如:

Impl a= new Impl("ei"); 
Impl m = Mockito.mock(Impl.class); 

when(m.someCall(Matchers.any(Object.class))).thenReturn(whereverYouWanna); 

或更具體:

when(m.someCall(Matchers.eq(24)).thenReturn(whereverYouWanna); 

是非常重要的是知道你以前的模擬可能會限制你的嘲笑代碼,希望它可以幫助你。

+0

我試試這個,但沒有結果相同的error.plz檢查編輯 – user3060230