New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCastException in javax.crypto caused by powermock #294

Closed
johanhaleby opened this Issue Jul 24, 2015 · 10 comments

Comments

Projects
None yet
2 participants
@johanhaleby
Collaborator

johanhaleby commented Jul 24, 2015

From Mark.Lehmacher@gmail.com on August 25, 2010 14:14:22

What steps will reproduce the problem? The following TestNG test will:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.IObjectFactory;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;

@test
public class MyTest
{

public final void testBla() throws Exception
{
final Cipher c = Cipher.getInstance( "AES/CBC/PKCS5Padding" );

  final SecretKey skey = new SecretKeySpec( new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} , "AES" ); 
  final IvParameterSpec iv = new IvParameterSpec( new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} );

  c.init( Cipher.ENCRYPT_MODE, skey, iv );

}

@ObjectFactory
public final IObjectFactory getObjectFactory()
{
return new org.powermock.modules.testng.PowerMockObjectFactory();
}

}

When running the test I get the following exception:

java.lang.ClassCastException: com.sun.crypto.provider.AESCipher cannot be cast to javax.crypto.CipherSpi
at javax.crypto.Cipher.a(DashoA13_..)
at javax.crypto.Cipher.init(DashoA13_..)
at javax.crypto.Cipher.init(DashoA13*..)
at mypackage.MyTest.testBla(MyTest.java:48)
... Removed 22 stack frames

When I leave out getObjectFactory() everything works.

As as sidenote, when getObjectFactory() is there and the test class itself is final (in my case MyTest), another exception ensues:

org.testng.TestNGException:
An error occurred while instantiating class com.aixigo.java.common.technology.core.anonymization.standard.MyTest: com.aixigo.java.common.technology.core.anonymization.standard.MyTest is final
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:329)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:71)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:90)
at org.testng.internal.TestNGClassFinder.(TestNGClassFinder.java:119)
at org.testng.TestRunner.initMethods(TestRunner.java:305)
at org.testng.TestRunner.init(TestRunner.java:251)
at org.testng.TestRunner.init(TestRunner.java:221)
at org.testng.TestRunner.(TestRunner.java:183)
at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:105)
at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:158)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:551)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:241)
at org.testng.SuiteRunner.run(SuiteRunner.java:195)
at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:903)
at org.testng.TestNG.runSuitesLocally(TestNG.java:872)
at org.testng.TestNG.run(TestNG.java:780)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:75)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:127)
Caused by: java.lang.RuntimeException: com.aixigo.java.common.technology.core.anonymization.standard.MyTest is final
at javassist.util.proxy.ProxyFactory.checkClassAndSuperName(ProxyFactory.java:748)
at javassist.util.proxy.ProxyFactory.makeSortedMethodList(ProxyFactory.java:772)
at javassist.util.proxy.ProxyFactory.computeSignature(ProxyFactory.java:781)
at javassist.util.proxy.ProxyFactory.createClass(ProxyFactory.java:392)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:2015)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:893)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:721)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:399)
at org.powermock.modules.testng.PowerMockObjectFactory.createTestClass(PowerMockObjectFactory.java:118)
at org.powermock.modules.testng.PowerMockObjectFactory.newInstance(PowerMockObjectFactory.java:79)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:317)
... 17 more

Again, without getObjectFactory() everything works. This issue contains a stripped down version of my real test, so leaving out the object factory is not really an option.

I am using:

powermock-mockito 1.4
(javassist-3.13.0.GA.jar, objenesis-1.2.jar)

TestNG 5.12

The problem arises within Eclipse (using TestNG plugin) as well as from executing tests from command line (vanilla TestNG).

Original issue: http://code.google.com/p/powermock/issues/detail?id=274

@johanhaleby

This comment has been minimized.

Show comment
Hide comment
@johanhaleby

johanhaleby Jul 24, 2015

Collaborator

From Mark.Lehmacher@gmail.com on August 25, 2010 05:19:34

Oops, I just noticed that the first (and main part of this issue) is a duplicate of #270. Sorry for that. However, the second problem (final Test-Classes resulting in exceptions) still seems valid.

Collaborator

johanhaleby commented Jul 24, 2015

From Mark.Lehmacher@gmail.com on August 25, 2010 05:19:34

Oops, I just noticed that the first (and main part of this issue) is a duplicate of #270. Sorry for that. However, the second problem (final Test-Classes resulting in exceptions) still seems valid.

@johanhaleby

This comment has been minimized.

Show comment
Hide comment
@johanhaleby

johanhaleby Jul 24, 2015

Collaborator

From jasoncow...@gmail.com on October 15, 2010 11:55:04

For others: the Crypto problem is solved by adding the following annotation at the class level:
@PowerMockIgnore({"javax.crypto.*" })

Collaborator

johanhaleby commented Jul 24, 2015

From jasoncow...@gmail.com on October 15, 2010 11:55:04

For others: the Crypto problem is solved by adding the following annotation at the class level:
@PowerMockIgnore({"javax.crypto.*" })

@johanhaleby

This comment has been minimized.

Show comment
Hide comment
@johanhaleby

johanhaleby Jul 24, 2015

Collaborator

From johan.ha...@gmail.com on October 15, 2010 12:23:33

Thanks for letting us know.

Status: WontFix

Collaborator

johanhaleby commented Jul 24, 2015

From johan.ha...@gmail.com on October 15, 2010 12:23:33

Thanks for letting us know.

Status: WontFix

@johanhaleby

This comment has been minimized.

Show comment
Hide comment
@johanhaleby

johanhaleby Jul 24, 2015

Collaborator

From sin1...@gmail.com on March 04, 2011 00:47:46

Annotation @PowerMockIgnore({"javax.crypto.*" }) did not work for me.
The one given below worked.
@PowerMockIgnore({"javax.crypto" })

Collaborator

johanhaleby commented Jul 24, 2015

From sin1...@gmail.com on March 04, 2011 00:47:46

Annotation @PowerMockIgnore({"javax.crypto.*" }) did not work for me.
The one given below worked.
@PowerMockIgnore({"javax.crypto" })

@johanhaleby

This comment has been minimized.

Show comment
Hide comment
@johanhaleby

johanhaleby Jul 24, 2015

Collaborator

From johan.ha...@gmail.com on March 04, 2011 00:52:52

What version are using?

Collaborator

johanhaleby commented Jul 24, 2015

From johan.ha...@gmail.com on March 04, 2011 00:52:52

What version are using?

@johanhaleby

This comment has been minimized.

Show comment
Hide comment
@johanhaleby

johanhaleby Jul 24, 2015

Collaborator

From michelle...@irismaps.com on March 14, 2014 10:44:29

@PowerMockIgnore({"javax.crypto.*" }) worked for me, thank you very much!

Collaborator

johanhaleby commented Jul 24, 2015

From michelle...@irismaps.com on March 14, 2014 10:44:29

@PowerMockIgnore({"javax.crypto.*" }) worked for me, thank you very much!

@johanhaleby

This comment has been minimized.

Show comment
Hide comment
@johanhaleby

johanhaleby Jul 24, 2015

Collaborator

From sjcevika...@gmail.com on August 25, 2014 22:06:07

I am doing Unit testing using Testng,mockito and powermockito
But i am getting a lots of problem.So please can u tell me proper configuration for that is eclips.what is the jar and plug-in is required

Collaborator

johanhaleby commented Jul 24, 2015

From sjcevika...@gmail.com on August 25, 2014 22:06:07

I am doing Unit testing using Testng,mockito and powermockito
But i am getting a lots of problem.So please can u tell me proper configuration for that is eclips.what is the jar and plug-in is required

@johanhaleby

This comment has been minimized.

Show comment
Hide comment
@johanhaleby

johanhaleby Jul 24, 2015

Collaborator

From tin...@163.com on September 16, 2014 01:09:16

@PowerMockIgnore({"javax.crypto.*" })
worked for me too, thank you!

Collaborator

johanhaleby commented Jul 24, 2015

From tin...@163.com on September 16, 2014 01:09:16

@PowerMockIgnore({"javax.crypto.*" })
worked for me too, thank you!

@johanhaleby

This comment has been minimized.

Show comment
Hide comment
@johanhaleby

johanhaleby Jul 24, 2015

Collaborator

From sujit.r...@gmail.com on December 22, 2014 04:31:36

such under-documented features like @PowerMockIgnore are real life-savers. Thx

Collaborator

johanhaleby commented Jul 24, 2015

From sujit.r...@gmail.com on December 22, 2014 04:31:36

such under-documented features like @PowerMockIgnore are real life-savers. Thx

@avibing

This comment has been minimized.

Show comment
Hide comment
@avibing

avibing Oct 3, 2017

After adding @PowerMockIgnore({"javax.crypto.*" }),
I am getting java.lang.IllegalStateException: Cipher not initialized
Any idea please.

avibing commented Oct 3, 2017

After adding @PowerMockIgnore({"javax.crypto.*" }),
I am getting java.lang.IllegalStateException: Cipher not initialized
Any idea please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment