Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception using SpringRunner with PowermockRunner

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?

like image 569
Abhishek Kumar Avatar asked May 12 '18 14:05

Abhishek Kumar


People also ask

Does JUnit 5 support Powermock?

To include powermock in our application, add the powermock-api-mockito2 and powermock-module-junit4 dependencies. Note that there is no official extension for JUnit 5. If you plan to use its reflection module, for example invoking the private methods, then we need to import powermock-reflect module as well.

How do you mock with Powermock?

Step 1: Create a class that contains a private method. We have created class with the name Utility and defined a private method and a public method (that returns the object of private method). Step 2: Create a JUnit test case named PowerMock_test for testing purpose.

What is <UNK>Runwith Powermockrunner?

PowerMock is a framework that extends other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more.

Will using @RunWith (powermockrunner) fail the test?

using @RunWith (PowerMockRunner.class) should not failed the test. What version of the product are you using? On what operating system? junit.version 4.11 Windows 7 Please provide any additional information below.

How to combine powermock and the springjunit4classrunner for integration testing?

This is achieved by using the new PowerMockRunnerDelegate annotation. For example you can delegate to Enclosed Runner, Parameterized Runner or the SpringJUnit4ClassRunner and still use the PowerMock functionality. Here’s a (contrived) example of combining PowerMock and the SpringJUnit4ClassRunner for integration testing “MyBean”.

How to create an example using powermock with Mockito?

To create an example using PowerMock, we need to go through the following steps. Step 1: Add the following PowerMock dependencies in pom.xml file. To use PowerMock with Mockito, we need to apply the following two annotations in the test: @RunWith (PowerMockRunner.class): It is the same as we have used in our previous examples.

What is the difference between @RunWith and @preparefortest in powermock?

@RunWith (PowerMockRunner.class): It is the same as we have used in our previous examples. The only difference is that in the previous example we have used MockitoUnitRunner.class, now we will use PowerMockRunner.class for enabling the PowerMockito APIs in the test. @PrepareForTest: It tells PowerMock to prepare some classes for testing.


1 Answers

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)
like image 170
Sasha Shpota Avatar answered Oct 11 '22 02:10

Sasha Shpota