java在keystorejunit测试用例中获取mockito异常

dwthyt8l  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(275)

我正在尝试运行junit,以便使用密钥库上载ssl证书。但是在执行测试用例的过程中模拟密钥库时,我面临着错误。我正在使用intellij ide。
这是我的junit类utilitytest.java

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.X509Certificate;
import static org.mockito.Matchers.any;
import static org.powermock.api.mockito.PowerMockito.mock;

@PrepareForTest(KeyStore.class)
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.management.*", "javax.security.*"})
public class UtilityTest {
    @InjectMocks
    Utility utility;
    @Test
    public void uploadTSCertificate() {
        MockMultipartFile file  = new MockMultipartFile("file","hello.txt",
                MediaType.TEXT_PLAIN_VALUE,"Hello, World!".getBytes());

        try {
            KeyStore keyStoreMock = mock(KeyStore.class);
            PowerMockito.mockStatic(KeyStore.class, invocation -> keyStoreMock);
            PowerMockito.when(KeyStore.getInstance(any())).thenReturn(keyStoreMock);
            PowerMockito.doNothing().when(keyStoreMock).load(any(InputStream.class), any(char[].class));
            X509Certificate cert = Mockito.mock(X509Certificate.class);
            PowerMockito.when(keyStoreMock.getCertificate(any())).thenReturn(cert);
            utility.uploadServerCertificate(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我的pom.xml

<powermock.api.version>1.7.4</powermock.api.version>
        <powermock.module.version>1.5.1</powermock.module.version>
        <powermock.common.version>1.6.5</powermock.common.version>
     <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>${powermock.api.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>${powermock.module.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito-common</artifactId>
            <version>${powermock.common.version}</version>
        </dependency>

错误:

我还从本地机器上删除了maven存储库并重建agian,仍然面临同样的问题。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题