以下のようなfinalメソッドを持つ非final具象クラスがあるとしましょう。
public class ABC {
public final String myMethod(){
return "test test";
}
}
を使用してmyMethod()
呼び出されたときに、他の何かを返すためにモックを作成することは可能ですか?ありがとうございましたjunit
Powermockito
以下のようなfinalメソッドを持つ非final具象クラスがあるとしましょう。
public class ABC {
public final String myMethod(){
return "test test";
}
}
を使用してmyMethod()
呼び出されたときに、他の何かを返すためにモックを作成することは可能ですか?ありがとうございましたjunit
Powermockito
これは機能します:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ABC.class)
public class ABCTest {
@Test
public void finalCouldBeMock() {
final ABC abc = PowerMockito.mock(ABC.class);
PowerMockito.when(abc.myMethod()).thenReturn("toto");
assertEquals("toto", abc.myMethod());
}
}