繁体   English   中英

Powermockito可以在非最终具体类中模拟最终方法吗?

[英]Can Powermockito mock final method in non-final concrete class?

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

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

是否可以模拟myMethod()在使用Powermockitojunit调用时返回其他内容? 谢谢

这有效:

@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());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM