vijayjcp / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 2 forks source link

ClassCastException in javax.crypto caused by powermock #274

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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.<init>(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.<init>(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 reported on code.google.com by Mark.Lehmacher@gmail.com on 25 Aug 2010 at 12:14

GoogleCodeExporter commented 8 years ago
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.

Original comment by Mark.Lehmacher@gmail.com on 25 Aug 2010 at 12:19

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
For others: the Crypto problem is solved by adding the following annotation at 
the class level:
@PowerMockIgnore({"javax.crypto.*" })

Original comment by jasoncow...@gmail.com on 15 Oct 2010 at 6:55

GoogleCodeExporter commented 8 years ago
Thanks for letting us know.

Original comment by johan.ha...@gmail.com on 15 Oct 2010 at 7:23

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

Original comment by sin1...@gmail.com on 4 Mar 2011 at 8:47

GoogleCodeExporter commented 8 years ago
What version are using?

Original comment by johan.ha...@gmail.com on 4 Mar 2011 at 8:52

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
@PowerMockIgnore({"javax.crypto.*" }) worked for me, thank you very much!

Original comment by michelle...@irismaps.com on 14 Mar 2014 at 5:44

GoogleCodeExporter commented 8 years ago
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

Original comment by sjcevika...@gmail.com on 26 Aug 2014 at 5:06

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
@PowerMockIgnore({"javax.crypto.*" })
worked for me too, thank you!

Original comment by tinily...@gmail.com on 16 Sep 2014 at 8:09

GoogleCodeExporter commented 8 years ago
such under-documented features like @PowerMockIgnore are real life-savers. Thx

Original comment by sujit.r...@gmail.com on 22 Dec 2014 at 12:31