使用Mockito存根最终方法 [英] use mockito to stub final method

查看:174
本文介绍了使用Mockito存根最终方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用具有最终方法的模拟程序。
所以我使用了powermock,但是它不起作用

I need to use a mock who has a final method. So i use powermock but it does not work

class B {
    public final int nb() {
        return 4;
    }
}

@RunWith(PowerMockRunner.class)
@PrepareForTest(B.class)
public class Exemple extends TestCase {
    @Test
    public void test() { 
        B b = PowerMockito.mock(B.class);

        PowerMockito.when(b.nb()).thenReturn(5);

        final int actualState = b.nb();

        assertEquals(5, actualState);

    }   
}

如果有人有解决方案, b $ b预先感谢您

if someone has a solution, thank you in advance

推荐答案

您的示例很好,应该可以正常工作。

Your example is fine, should work without any problem.

Tests Passed: 1 passed in 0,226 s

我已经用

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
</dependency> 

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.5.2</version>
</dependency>

这篇关于使用Mockito存根最终方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆