17

以下のようなfinalメソッドを持つ非final具象クラスがあるとしましょう。

public class ABC {
  public final String myMethod(){
      return "test test";
  }
}

を使用してmyMethod()呼び出されたときに、他の何かを返すためにモックを作成することは可能ですか?ありがとうございましたjunitPowermockito

4

1 に答える 1

32

これは機能します:

@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());
    }
}
于 2012-08-27T12:32:50.557 に答える