使用SpringRunner和PowermockRunner的异常

时间:2018-05-12 14:39:16

标签: java javamail powermock powermockito spring-boot-test

我正在尝试测试JavaMail api并使用SpringRunner和PowerMockRunner,但它失败了。

@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
  }
}

我得到了这个例外:

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

如果我删除了@PowerMockIgnore(value = {" javax.management。*"}),那么我将收到此异常:

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"

使用的依赖版本是:

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

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

它看起来像一个bug。

在我的案例中帮助的解决方案是缩小加载的配置。

尝试指定要加载的最小配置集:

@SpringBootTest(classes = SomeSpesificConfiguration.class)

答案 1 :(得分:0)

  1. 使用powermock-api-mockito2

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