如何使用 NoSuchAlgorithmException 和 KeyStoreException 覆盖 JUnit 的

How to cover block catch by JUnit with NoSuchAlgorithmException and KeyStoreException(如何使用 NoSuchAlgorithmException 和 KeyStoreException 覆盖 JUnit 的块捕获)
本文介绍了如何使用 NoSuchAlgorithmException 和 KeyStoreException 覆盖 JUnit 的块捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想介绍 getKeyStore() 方法,但我不知道如何介绍 NoSuchAlgorithmException、KeyStoreException、UnrecoverableKeyException 和 CertificateException 的 catch 块.我的方法是:

I want to cover getKeyStore() methode, But I don't know how to cover catch block for NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException and CertificateException. My methode is :

public static KeyManagerFactory getKeyStore(String keyStoreFilePath)
        throws IOException {
    KeyManagerFactory keyManagerFactory = null;
    InputStream kmf= null;
    try {
        keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keystoreStream = new FileInputStream(keyStoreFilePath);
        keyStore.load(keystoreStream, "changeit".toCharArray());
        kmf.init(keyStore, "changeit".toCharArray());
    } catch (NoSuchAlgorithmException e) {
        LOGGER.error(ERROR_MESSAGE_NO_SUCH_ALGORITHM + e);
    } catch (KeyStoreException e) {
        LOGGER.error(ERROR_MESSAGE_KEY_STORE + e);
    } catch (UnrecoverableKeyException e) {
        LOGGER.error(ERROR_MESSAGE_UNRECOVERABLEKEY + e);
    } catch (CertificateException e) {
        LOGGER.error(ERROR_MESSAGE_CERTIFICATE + e);
    } finally {
        try {
            if (keystoreStream != null){
                keystoreStream.close();
            }
        } catch (IOException e) {
            LOGGER.error(ERROR_MESSAGE_IO + e);
        }
    }
    return kmf;
}

<小时>

我该怎么做?


How do I do it?

推荐答案

你可以mock try 块中的任何一句话来抛出你想捕获的异常.

You can mock any sentence of the try block to throw the exception you want to catch.

模拟 KeyManagerFactory.getInstance 调用以抛出 NoSuchAlgorithmException 的示例.在这种情况下,您将覆盖第一个 catch 块,您必须对捕获的其他异常(KeyStoreException、UnrecoverableKeyException 和 CertificateException)执行相同操作

Example mocking the KeyManagerFactory.getInstance call to throw NoSuchAlgorithmException. In that case, you will cover the first catch block, you have to do the same with other exceptions caught (KeyStoreException, UnrecoverableKeyException and CertificateException)

你可以这样做(因为方法 getInstancestatic,你必须使用 PowerMockito 而不是 Mockito,请参阅此 问题了解更多信息)

You can do as follows (as method getInstance is static, you have to use PowerMockito instead Mockito, see this question for more info)

@PrepareForTest(KeyManagerFactory.class)
@RunWith(PowerMockRunner.class)
public class FooTest {

   @Test
   public void testGetKeyStore() throws Exception {
      PowerMockito.mockStatic(KeyManagerFactory.class);
      when(KeyManagerFactory.getInstance(anyString())).thenThrow(new NoSuchAlgorithmException());
   }
}

希望对你有帮助

这篇关于如何使用 NoSuchAlgorithmException 和 KeyStoreException 覆盖 JUnit 的块捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)