<tfoot id='LYGkp'></tfoot>

      <small id='LYGkp'></small><noframes id='LYGkp'>

    1. <i id='LYGkp'><tr id='LYGkp'><dt id='LYGkp'><q id='LYGkp'><span id='LYGkp'><b id='LYGkp'><form id='LYGkp'><ins id='LYGkp'></ins><ul id='LYGkp'></ul><sub id='LYGkp'></sub></form><legend id='LYGkp'></legend><bdo id='LYGkp'><pre id='LYGkp'><center id='LYGkp'></center></pre></bdo></b><th id='LYGkp'></th></span></q></dt></tr></i><div id='LYGkp'><tfoot id='LYGkp'></tfoot><dl id='LYGkp'><fieldset id='LYGkp'></fieldset></dl></div>
      <legend id='LYGkp'><style id='LYGkp'><dir id='LYGkp'><q id='LYGkp'></q></dir></style></legend>
        • <bdo id='LYGkp'></bdo><ul id='LYGkp'></ul>

        如何使用 NoSuchAlgorithmException 和 KeyStoreException 覆

        时间:2023-09-25

            • <small id='YM4D7'></small><noframes id='YM4D7'>

                  <tbody id='YM4D7'></tbody>
                <legend id='YM4D7'><style id='YM4D7'><dir id='YM4D7'><q id='YM4D7'></q></dir></style></legend>

                <i id='YM4D7'><tr id='YM4D7'><dt id='YM4D7'><q id='YM4D7'><span id='YM4D7'><b id='YM4D7'><form id='YM4D7'><ins id='YM4D7'></ins><ul id='YM4D7'></ul><sub id='YM4D7'></sub></form><legend id='YM4D7'></legend><bdo id='YM4D7'><pre id='YM4D7'><center id='YM4D7'></center></pre></bdo></b><th id='YM4D7'></th></span></q></dt></tr></i><div id='YM4D7'><tfoot id='YM4D7'></tfoot><dl id='YM4D7'><fieldset id='YM4D7'></fieldset></dl></div>
                • <bdo id='YM4D7'></bdo><ul id='YM4D7'></ul>
                  <tfoot id='YM4D7'></tfoot>
                • 本文介绍了如何使用 NoSuchAlgorithmException 和 KeyStoreException 覆盖 JUnit 的块捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想介绍 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 的块捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么模拟不能与 AsyncTask 一起工作? 下一篇:Powermock 和 Mockito.在模拟和存根同一类时避免对类

                  相关文章

                  最新文章

                      <i id='pO6Sg'><tr id='pO6Sg'><dt id='pO6Sg'><q id='pO6Sg'><span id='pO6Sg'><b id='pO6Sg'><form id='pO6Sg'><ins id='pO6Sg'></ins><ul id='pO6Sg'></ul><sub id='pO6Sg'></sub></form><legend id='pO6Sg'></legend><bdo id='pO6Sg'><pre id='pO6Sg'><center id='pO6Sg'></center></pre></bdo></b><th id='pO6Sg'></th></span></q></dt></tr></i><div id='pO6Sg'><tfoot id='pO6Sg'></tfoot><dl id='pO6Sg'><fieldset id='pO6Sg'></fieldset></dl></div>
                      <tfoot id='pO6Sg'></tfoot>

                      <small id='pO6Sg'></small><noframes id='pO6Sg'>

                        <bdo id='pO6Sg'></bdo><ul id='pO6Sg'></ul>
                    1. <legend id='pO6Sg'><style id='pO6Sg'><dir id='pO6Sg'><q id='pO6Sg'></q></dir></style></legend>