Sunday, April 16, 2017

SecurityProvider in Mockito Tests when run alongside Robolectric

Leave a Comment

We haven an Android project where we use MockitoTestRunner and RobolectricTestRunner for different kinds of tests.

I have written a set of unit test that are concerned with SSL, thus loading certs/keystores/truststores, etc. For this I used the MockitoJUnitRunner and added the Bouncycastle provider programmatically as such:

Security.insertProviderAt(new BouncyCastleProvider(), 1); 

Now, these tests run perfectly fine when run on their own - e.g. when I directly run single methods from the test classes, or run these classes from the Project tree menu, they work just fine.

But when I run said tests along side ANY test which uses the RobolectricTestRunner (such as if I just run all the tests in my project together before committing), I get the following exception:

java.io.IOException: error constructing MAC: java.lang.SecurityException: JCE cannot authenticate the provider BC 

I'm baffled. How will the testrunner used in one test class affect the run of other classes, especially if we use a different test runner?


Additional info:

  • The exception only occurs once I actually try do do something with the BC provider (e.g. the first time the test tries to load a PKCS12 certificate) - the insertProviderAt(...) call itself seems to pass fine...
  • Also when I print out the list of providers for each test run, I see that Robolectric already has a BC provider in there, but is still failing when I try to use it.
  • Also if I don't add the BC provider, the tests still fail with the same error when run in a test suite alongside Robolectric tests. When run alone they fail with java.security.NoSuchProviderException: no such provider: BC, as we're specifying the provider explicitly.

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment