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?

houchker
  • 103
  • 1
  • 2
  • 11
  • I am not sure it needs covering. However, maybe I would create `handleException(Exception)` or if i were using java7 maybe I would catch multiple exceptions (http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html). I am not sure this logging is worth effort covering. – Vytenis Bivainis Oct 14 '14 at 21:01

1 Answers1

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

Community
  • 1
  • 1
troig
  • 7,072
  • 4
  • 37
  • 63
  • 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 – houchker Oct 14 '14 at 14:49
  • You are mocking another class, KeyStore. Maybe you've forgotten to change the @PrepareForTest annotation. Should be: @PrepareForTest(KeyStore.class) – troig Oct 14 '14 at 14:51
  • 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 – houchker Oct 14 '14 at 14:58
  • I think it's because KeyStore is a singleton [see](http://stackoverflow.com/questions/2302179/mocking-a-singleton-class) – troig Oct 14 '14 at 15:32
  • @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? – adbdkb Sep 13 '18 at 08:30
  • @adbdkb maybe you could post a new question with all the details? – troig Sep 13 '18 at 08:43
  • @triog, thanks. I already have a question posted at https://stackoverflow.com/questions/52267168/how-to-simulate-caught-and-logged-exceptions-inside-a-method-and-create-junit-te on similar lines before I found this question and answer. I will update the question with class methid and my junit method code and add a comment addressed to you. Will that work? – adbdkb Sep 13 '18 at 08:57
  • Great, I'll try to have a look asap – troig Sep 13 '18 at 09:03
  • @troig - I have updated the other question with full example and error. Thanks again for helping out. The updates are under -- Update 2 - Another Example – adbdkb Sep 13 '18 at 12:39
  • @troig - Did you have an opportunity to look at my issue at https://stackoverflow.com/questions/52267168/how-to-simulate-caught-and-logged-exceptions-inside-a-method-and-create-junit-te ? Thanks again for your help – adbdkb Sep 17 '18 at 18:23