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:
Can someone help?
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.
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.
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.
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.
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”.
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.
@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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With