2014-11-03 65 views
0

請幫幫忙,這個模擬是不工作:無法通過嘲弄一個jmockit私有方法

class ClassBeingTested { 

    private AnotherClass anotherClass; 

    public void someMethod() { 
     int ans = anotherClass.targetMethod(5); 
     // Use ans here 
    } 
} 

//我的測試

ClassBeingTested classObject; 
AnotherClass anotherClassObject; 

@Before 
public void setup() { 
     // Initialize anotherClassObject here 

     classObject = new ClassBeingTested(anotherClassObject); 

     new NonStrictExpectations(anotherClassObject) {{ 
      invoke(anotherClassObject, "targetMethod", Integer.class); result = 100; 
     }}; 
} 

@Test 
public void testSomeMethod() { 
    classObject.someMethod(); 
} 

回答

0

嘲諷,只要我更換Integer.class與合作實際預期的整數值。

new NonStrictExpectations(anotherClassObject) {{ 
     invoke(anotherClassObject, "targetMethod", 5); result = 100; 
    }}; 
0

這種用JMockit嘲笑的方式怎麼樣?

import mockit.Injectable; 
import mockit.Tested; 
... 


@Tested 
ClassBeingTested classObject; 
@Injectable 
AnotherClass anotherClassObject; 

@Before 
public void setup() { 
    new Expectations() {{ 
     anotherClassObject.targetMethod(anyInt); result = 100; 
    }}; 
} 

@Test 
public void testSomeMethod() { 
    classObject.someMethod(); 
}