Search code examples
javajunitmockitokeystorepowermockito

Writing test case for keystore.load(InputStream,password)


I have been trying to write a test case for a SSLContextCongif.java class. There's a code section for which I have been having a hard time writing test case for:

KeyStore ks = KeyStore.getInstance("JKS");

ks.load(new FileInputStream(trustStore), password);

TrustManagerFactory trustManagerFactory = 
TrustManagerFactory.getInstance("PKIX");
trustManagerFactory.init(ks);

Test section i have written for this:

keystore=PowerMockito.mock(KeyStore.class);
trustManagerFactory =mock(TrustManagerFactory.class); 

String keys="mockedkeys";     

keystore = KeyStore.getInstance("JKS");
keystore.load(File(keys), password);

trustManagerFactory= TrustManagerFactory.getInstance("PKIX");
trustManagerFactory.init(keystore);

But here's the issue, the test case throws an exception at ks.load() An exception occurred during setting up SSL context. {}


Solution

  • There is no way i could find to mock keystore.load() function. So I passed dummy keystore file with it's respective password. Worked like a charm.