Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

junit - How do I mock a static method using PowerMockito?

I am using:

    <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);

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

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

I've tried:

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You might want try changing your line @PrepareForTest statement.

From:

@PrepareForTest(KeyPairGenerator.class)

to:

@PrepareForTest(ClassThatCallsTheStaticMethod.class)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...