8

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?

Abhishek Kumar
  • 3,328
  • 2
  • 18
  • 31
  • 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 May 23 '18 at 07:42
  • 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](https://stackoverflow.com/questions/18385074/powermock-preparefortest-annotation-causing-problems-with-amazonsqsclient-constr), maybe can help. – troig May 23 '18 at 08:57
  • @Tobb I will try to remove these dependencies if nothing works out – Abhishek Kumar May 23 '18 at 10:14
  • @troig I have tried that – Abhishek Kumar May 23 '18 at 10:14

2 Answers2

1

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)
Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148
  • Can you be specific. I have to run the integration test using JavaMail APIs i.e. I have to create a proper TCP connection (SSL) and here things are failing – Abhishek Kumar May 23 '18 at 10:16
0
  1. Use powermock-api-mockito2

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

chandra
  • 500
  • 4
  • 12