0
我必須爲某些方法進行一些jUnit測試(使用mocks),並且我無法更改源代碼。有沒有可能在不更改源代碼的情況下改變函數的行爲(可能是模擬)?junit測試中的模擬方法
看起來是一個直截了當的例子: 類A
和B
是源代碼(無法更改它們)。當我在B
中調用方法時,我想改變A
的行爲。有任何想法嗎?
public class A {
public String run(){
return "test";
}
}
public class B extends A {
public void testing() {
String fromA = run(); //I want a mocked result here
System.out.println(fromA);
}
}
public class C {
B mockB = null;
@Test
public void jUnitTest() {
mockB = Mockito.mock(B.class);
// And here i want to call testing method from B but with a "mock return" from run()
}
}
請發佈至少需要編譯的示例代碼。 –