tiebin-zhang / powermock

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

PowerMock fails with InitializerException if ant version 1.8.2+ is used. #395

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Use Ant version 1.8.2+
2. Use ant-junit target to run tests. 
3. All testcases that uses @PrepareTest ( tests final or static classes) fail 
with InitializerException - Cannot subclass final class.

What is the expected output? What do you see instead?
 With Ant1.7.2 all test passes , when I use Ant 1.8.2+ tests fails with above error.

What version of the product are you using? On what operating system?
Powermock version 1.4.10

Original issue reported on code.google.com by praya...@gmail.com on 19 Jun 2012 at 8:46

GoogleCodeExporter commented 9 years ago
I get the error even if I use the latest binaries of Powermock and associated 
dependencies. I am attaching the error stack

java.lang.IllegalArgumentException: Cannot subclass final class class 
...SessionManagerHelper at 
net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:446) at 
net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.jav
a:25) at 
net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
 at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377) at 
net.sf.cglib.proxy.Enhancer.createClass(Enhancer.java:317) at 
org.easymock.internal.ClassProxyFactory.createProxy(ClassProxyFactory.java:181) 
at org.easymock.internal.MocksControl.createMock(MocksControl.java:60) at 
org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:2212) at 
org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2163) at 
org.powermock.api.easymock.PowerMock.mockStatic(PowerMock.java:287) at 
...ApplicationStateHelperTest.setUp(Unknown Source)

Original comment by praya...@gmail.com on 19 Jun 2012 at 9:00

GoogleCodeExporter commented 9 years ago
Perhaps there's a JUnit conflict in the classpath?

Original comment by johan.ha...@gmail.com on 13 Jul 2012 at 7:05

GoogleCodeExporter commented 9 years ago
Please use Powermock to resolve this issue.

For ex: if u are trying to mock URL (java.net.URL) class using easymock it will 
throw the above mentioned exception. Use Powermock instead of easymock.

URL url = EasyMock.createMock(URL.class) --> Will throw exception
URL url = PowerMock.createMock(URL.class) --> Works fine (But do not forget to 
add the class in @PrepareForTest).

Original comment by bharath...@gmail.com on 27 Jul 2012 at 12:24

GoogleCodeExporter commented 9 years ago
I am facing the same problem with Ant 1.8.2+. In eclipse and Ant 1.7.1 every 
thing is working fine but not working in Ant 1.8.2 and above. I also tried 
PowerMock.createMock instead Mockito.createMock and @PrepareForTest on class 
level but no luck.

PowerMockito.mockStatic(StringUtils.class);
PowerMockito.when(StringUtils.getTerm("abcd")).thenReturn("xyz");

Original comment by ashishdu...@gmail.com on 18 Sep 2012 at 3:35

GoogleCodeExporter commented 9 years ago
Ehi guys ? Have you solved ?
I'm getting a lot of problem with Ant and @PrepareTest !

Original comment by Benedett...@gmail.com on 15 Jan 2014 at 11:30

GoogleCodeExporter commented 9 years ago
Is your test case class extending from TestCase? 
Note that if you are using a mixed Junit3/4 style test then Ant will prioritise 
v3 over v4 and fail to invoke the PowerMock runner.
I had a very similar problem and sorted it out by converting the test case 
class  to annotations only.
Please see here for a related example that might help you: 
http://stackoverflow.com/questions/7796295/running-junit-through-ant-does-not-se
em-to-use-custom-class-runner 

Original comment by leonardo...@gmail.com on 19 Aug 2014 at 9:10