tiebin-zhang / powermock

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

Error while mocking Static method #262

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I am using 1.2.5 version of power mock

The operating system is windows.

I have attached the error, which i am getting while trying to mock a static 
method, which looks like:

public class YUtil extends Xutil{
 public static Long getPort(Long serviceid) throws Exception{
       Long cellEndPort = Xutil.getPortsForCircuit(circuitId);
 }
}

I am mocking it like below

@PrepareForTest({YUtil.class})
mockStatic(YUtil.class);
expect(YUtil.getPort(123L)).andReturn(20L);

What i have noticed is its going inside the YUtil and also inside Xutil.
This means my expect is not working.

I have done replayAll.

The java assist that i am using is javassist-3.9.0.GA

One more observation : Xutil.class is too large(About 6000 lines).

Original issue reported on code.google.com by vichhu...@yahoo.com on 10 Jun 2010 at 7:22

Attachments:

GoogleCodeExporter commented 9 years ago
I just reduced the size of Xutil.class by retaining only getPortsForCircuit 
method which i require and tried running, its working now.

It fails with the attached error only when you do "Coverage As" JUnit Test, 
instead if you do "Run As" Junit Test it works fine even with the large file 
size.

please comment on this.

Original comment by vichhu...@yahoo.com on 10 Jun 2010 at 8:01

GoogleCodeExporter commented 9 years ago
The issue was with PrepareForTest, where i was giving YUtil which extends Xutil 
which is very large.
Now i am not preparing it for test,instead i am doing 
SuppressStaticInitializationFor
This has solved the problem.

Note: still i have to do mockStatic(YUtil.class);

Original comment by vichhu...@yahoo.com on 10 Jun 2010 at 9:33

GoogleCodeExporter commented 9 years ago
Hmm, I know CGLib has a maximum class size limit and this is hard to get 
around. Suppressing the static initializer may have the consequence that other 
classes aren't loaded which may have causes your problem. I think your 
workaround is ok, but you should consider refactoring your code.

Original comment by johan.ha...@gmail.com on 14 Jul 2010 at 3:02