加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 05:18:42 所属栏目:Java 来源:网络整理
导读:假设我有一个非最终的具体类,最后一个方法如下. public class ABC { public final String myMethod(){ return "test test"; }} 使用Powermockito在junit中调用myMethod()可以返回别的东西吗?谢谢 解决方法 这样做: @RunWith(PowerMockRunner.class)@Prepar
假设我有一个非最终的具体类,最后一个方法如下.
public class ABC {
  public final String myMethod(){
      return "test test";
  }
}

使用Powermockito在junit中调用myMethod()可以返回别的东西吗?谢谢

解决方法

这样做:
@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());
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读