Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对内部模拟对象的模拟方法调用_Java_Unit Testing_Powermockito - Fatal编程技术网

Java 对内部模拟对象的模拟方法调用

Java 对内部模拟对象的模拟方法调用,java,unit-testing,powermockito,Java,Unit Testing,Powermockito,我正在为我的类编写一个测试用例,它使用KeyStore读取证书和密钥。我已经模拟了KeyStore对象,还试图模拟KeyStore.getCertificate(),但是没有调用这个模拟方法。我犯了一个错误 下面的类代表我要测试的类 public class MyClass { public void methodToTest() { String keystoreFilename = "config/keystore.jks"; String alias

我正在为我的类编写一个测试用例,它使用
KeyStore
读取证书和密钥。我已经模拟了
KeyStore
对象,还试图模拟
KeyStore.getCertificate()
,但是没有调用这个模拟方法。我犯了一个错误

下面的类代表我要测试的类

public class MyClass {
    public void methodToTest() {
        String keystoreFilename = "config/keystore.jks";
        String alias = "alise";
        char[] password = "password".toCharArray();
        KeyStore keystore = getKeyStore(keystoreFilename, password);
        Certificate cert = keystore.getCertificate(alias);
        Key key = keystore.getKey(alias, password);
    }

    public KeyStore getKeyStore(String keystoreFilename, char []password) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
        //TODO Cert and Key - remove this once able to get this from TA_DB APIs
        FileInputStream fIn = new FileInputStream(keystoreFilename);
        KeyStore keystore = KeyStore.getInstance("JKS");
        keystore.load(fIn, password);
        return keystore;
    }
}
我的测试用例是这样的

@Test
public void testMethod() throws Exception{
    MyClass testClass = PowerMockito.mock(MyClass.class);
    //Mock certificate, key and KeyStore
    KeyStore keyStore = PowerMockito.mock(KeyStore.class);
    Certificate certificate = PowerMockito.mock(Certificate.class);
    Key key = PowerMockito.mock(Key.class);
    PowerMockito.when(keyStore.getCertificate(anyString())).thenReturn(certificate);
    PowerMockito.when(certificate.getEncoded()).thenReturn(TestConstants.RESPONSE_DATA);
    PowerMockito.when(keyStore.getKey(anyString(), anyObject())).thenReturn(key);
    PowerMockito.when(key.getEncoded()).thenReturn(TestConstants.RESPONSE_DATA);
    PowerMockito.when(testClass.getKeyStore(anyString(), any(char[].class))).thenReturn(keyStore);

    testClass.methodToTest();
}
但我得到了一个错误

原因:java.security.KeyStoreException:未初始化的密钥库位于 getCertificate(KeyStore.java:1079)位于 methodToTest(MyClass.java:7)


如果我做错了什么,请通知我,或者实现我的要求的确切方法是什么。

看起来像是对
密钥库的模仿。getCertificate
方法不起作用。不是Powermock方面的专家,但也许这与“getCertificate”方法是最终方法有关?只是猜测而已。如果是这样的话可能会有所帮助。嗨,詹尼斯,你的猜测是正确的,我试过你建议的帖子中提到的方法。但我还是犯了同样的错误。我添加了一个标记@PrepareForTest(KeyStore.class),看起来像是对
KeyStore.getCertificate
方法的模拟,但没有起作用。不是Powermock方面的专家,但也许这与“getCertificate”方法是最终方法有关?只是猜测而已。如果是这样的话可能会有所帮助。嗨,詹尼斯,你的猜测是正确的,我试过你建议的帖子中提到的方法。但我还是犯了同样的错误。我添加了一个标记@PrepareForTest(KeyStore.class)