SSLContext.getInstance中的NoSuchAlgorithmException

dop*_*ime 3 java ssl

我得到NoSuchAlgorithmException以下代码:

 @RunWith(PowerMockRunner.class)
 @PrepareForTest({CloudWatchHelper.class})
 class MyTest {
 ....
 final SSLContext sslcontext = SSLContext.getInstance("TLS");
 ...
 }
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

[junit] class configured for SSLContext: sun.security.ssl.SSLContextImpl$TLSContext not a SSLContext
[junit] java.security.NoSuchAlgorithmException: class configured for SSLContext: sun.security.ssl.SSLContextImpl$TLSContext not a SSLContext
[junit]     at sun.security.jca.GetInstance.checkSuperClass(GetInstance.java:260)
[junit]     at sun.security.jca.GetInstance.getInstance(GetInstance.java:237)
[junit]     at sun.security.jca.GetInstance.getInstance(GetInstance.java:164)
[junit]     at javax.net.ssl.SSLContext.getInstance(SSLContext.java:156)
Run Code Online (Sandbox Code Playgroud)

我想知道背后的原因是什么NoSuchAlgorithmException

Nat*_*ski 5

在我的测试类中添加以下批注为我解决了此问题:

@PowerMockIgnore({ "javax.net.ssl.*", "javax.security.*" })
Run Code Online (Sandbox Code Playgroud)

  • 值得注意的是,下面的评论中提到了这一点,但是将此作为答案可能是比接受的答案更普遍有用的修复,这是特定于“CloudWatchHelper”的答案,但使用 PowerMock 的许多不同类都可能会发生此错误。 (2认同)

dop*_*ime -5

我意识到我得到例外是因为:

 @RunWith(PowerMockRunner.class)
 @PrepareForTest({CloudWatchHelper.class})
Run Code Online (Sandbox Code Playgroud)

  • 实际上,我通过这个[帖子](http://mathieuhicauber-java.blogspot.co.uk/2013/07/powermock-and-ssl-context.html)找到了解决方案。最重要的是,您需要添加:`@PowerMockIgnore({ "javax.net.ssl.*", "javax.security.*" })`。 (4认同)