8
votes

I am trying to test a JavaMail api and using SpringRunner and PowerMockRunner but it is failing.

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore(value = {"javax.management.*"})
@SpringBootTest
public class BaseITest {

  @PrepareForTest(value = {MyStaticHelper.class})
  @Test
  public void testListFolders() {
     // mock static method
     // Use JavaMail API
  }
}

I am getting this exception:

javax.mail.MessagingException: java.security.NoSuchAlgorithmException: class configured for SSLContext: sun.security.ssl.SSLContextImpl$DefaultSSLContext not a SSLContext

If I remove @PowerMockIgnore(value = {"javax.management.*"}) then I am getting this exception:

Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of org/powermock/core/classloader/MockClassLoader) previously initiated loading for a different type with name "javax/management/MBeanServer"

The dependency versions used are:

  • powermock-api-mockito: 1.7.1
  • powermock-module-junit4: 1.7.1
  • mockito-all: 2.0.2-beta
  • mockito-core: 2.8.9

Can someone help?

2
Not a solution, but you have now experienced the problem with using PowerMockito. In my experience, it's better to create code that is testable without using PowerMock than trying to fix this type of error (I got some byte-code errors, gave it a big "NOPE!" and slammed @Ignore on the tests..) - Tobb
Have you tried ignoring both? @PowerMockIgnore(value = {"javax.management.*", "sun.security.ssl.*"}). I had a very similar issue and it worked for me. See this other SO question, maybe can help. - troig
@Tobb I will try to remove these dependencies if nothing works out - Abhishek Kumar
@troig I have tried that - Abhishek Kumar

2 Answers

1
votes

It looks like a bug.

The solution that helped in my case was narrowing the loaded configurations.

Try to specify minimal set of configurations to load:

@SpringBootTest(classes = SomeSpesificConfiguration.class)
0
votes
  1. Use powermock-api-mockito2

  2. Try @PowerMockIgnore("javax.net.ssl.*") https://groups.google.com/forum/#!topic/powermock/v4nreP2AnOQ