17

假设我有一个非最终的具体类,其最终方法如下所示。

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

在using中调用时是否可以模拟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 回答