1

I am moving a app to Java 11 which uses encryption. When I try to run the tests I get the following error.

java.lang.ClassCastException: 
class com.sun.crypto.provider.AESCipher$General 
cannot be cast to class javax.crypto.CipherSpi 
(com.sun.crypto.provider.AESCipher$General is in module 
java.base of loader 'bootstrap'; 
javax.crypto.CipherSpi is in unnamed module of loader
org.powermock.core.classloader.javassist.JavassistMockClassLoader@4c1d9d4b)

I don't understand what it means or how to resolve it.

Zack Macomber
  • 6,682
  • 14
  • 57
  • 104
Tim McGinnis
  • 83
  • 1
  • 7
  • Possibly related - https://stackoverflow.com/q/60872965/1098361 – Zack Macomber Feb 23 '21 at 14:08
  • After reviewing the possibly related post It seems to me that the AESCipher$General comes from java.base classloader and it is finding javax.crypto.CipherSpi in the powermock.core classloader. If I understand it correctly. I have looked in the powermock dependencies (actually all my dependencies) and I cannot find one that contains javax.crypto.CipherSpi. – Tim McGinnis Feb 23 '21 at 18:46
  • Have you tried a simple test where you cast `AESCipher$General` to `CipherSpi` all by itself? Breaking things down as small as you can can help resolve issues. – Zack Macomber Feb 23 '21 at 19:12
  • When I try to create the class it says the com.sun.crypto.provider package does not exist. – Tim McGinnis Feb 24 '21 at 13:46
  • I am assuming that is because java.base does not export that package. – Tim McGinnis Feb 24 '21 at 13:48
  • 1
    I re-configured my test case and have identified the issue as being a problem when using the @RunWIth(PowerMockRunner.class) annotation. If I do not use this annotation the test runs. If I add the annotation it throws the error listed above. – Tim McGinnis Feb 24 '21 at 19:42
  • Good troubleshooting and thanks for letting the SO community know! May help others or yourself in the future! – Zack Macomber Feb 24 '21 at 21:01

2 Answers2

1

After some helpful direction I was able to track the issue down to happening only when the @RunWith(PowerMockRunner.class) was used. To resolve the issue I added the @PowerMockIgnore({"javax.crypto.*"}) annotation. This resolved all the errors and the test now works as expected.

Tim McGinnis
  • 83
  • 1
  • 7
0

This helped, mockito was trying to mock some these objects which was leading to failure. Adding @PowerMockIgnore does help.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 08 '22 at 09:32