2

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?

1

1 Answer 1

3

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

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)

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

Hope it helps

Sign up to request clarification or add additional context in comments.

10 Comments

Hi, With tour test it work well. But when I did same for KeyStoreException. didn't work. My code Is : @Test public void testGetStore() throws Exception { PowerMockito.mockStatic(KeyStore.class); PowerMockito.when(KeyStore.getInstance(Mockito.anyString())).thenThrow(new KeyStoreException()); } . Manny thanks
You are mocking another class, KeyStore. Maybe you've forgotten to change the @PrepareForTest annotation. Should be: @PrepareForTest(KeyStore.class)
No. I made KeyStore in @PrepaerForTest. I thnik the problem is the KeyStore.getInstance(Mockito.anyString()) didnt throw KeyStoreException. The test pass but didnt catch the exception
I think it's because KeyStore is a singleton see
@troig, I tried the above approach, but it is not working for me. Unfortunately, I cannot add the complete code in the comment part and it is not the answer, so don't want to add the details of my attempt as an answer. I realise this is an old question but it is exactly same question / problem I have as this question. What would be the best way for me to provide details of my scenario?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.