如何使用 PowerMockito 模拟静态方法? [英] How do I mock a static method using PowerMockito?

查看:45
本文介绍了如何使用 PowerMockito 模拟静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>${powermock.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>${powermock.version}</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

我正在尝试使用以下调用测试一段代码:

I am attempting to test a piece of code with the following call:

final KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEY_PAIR_ALGORITHM, DEFAULT_PROVIDER);

这两个常量都是String类型的,所以我在调用:

The two constants are of type String, so I'm calling:

java.security.KeyPairGenerator.getInstance(String algorithm, String provider)

我试过了:

import static org.mockito.Mockito.when;

import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(KeyPairGenerator.class)
public class TestClass {

    private static final String DEFAULT_PROVIDER = "BC";
    private static final String KEY_PAIR_ALGORITHM = "RSA";

    @Test
    public void test() throws NoSuchAlgorithmException, NoSuchProviderException {
        final KeyPairGenerator kpg = Mockito.mock(KeyPairGenerator.class);
        PowerMockito.mockStatic(KeyPairGenerator.class);
        when(KeyPairGenerator.getInstance(KEY_PAIR_ALGORITHM, DEFAULT_PROVIDER)).thenReturn(kpg);
    }
}

我尝试将 when(KeyPairGenerator.getInstance(KEY_PAIR_ALGORITHM, DEFAULT_PROVIDER)).thenReturn(kpg); 替换为 PowerMockito.doReturn(kpg).when(KeyPairGenerator.class); 但似乎都没有让我到达我想要的地方,因为我仍然得到 NoSuchProviderException.任何见解将不胜感激.

I've tried replacing when(KeyPairGenerator.getInstance(KEY_PAIR_ALGORITHM, DEFAULT_PROVIDER)).thenReturn(kpg); with PowerMockito.doReturn(kpg).when(KeyPairGenerator.class); but neither seem to get me where I want as I'm still getting the NoSuchProviderException. Any insight would be appreciated.

推荐答案

您可能想尝试更改您的行 @PrepareForTest 语句.

You might want try changing your line @PrepareForTest statement.

发件人:

@PrepareForTest(KeyPairGenerator.class)

到:

@PrepareForTest(ClassThatCallsTheStaticMethod.class)

这篇关于如何使用 PowerMockito 模拟静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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