This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Testing and the fly likes PowerMock: StackOverFlow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "PowerMock: StackOverFlow" Watch "PowerMock: StackOverFlow" New topic
Author

PowerMock: StackOverFlow

sam backet
Greenhorn

Joined: Sep 02, 2011
Posts: 22

Hello, I have the following problem:

I wanted to mock a private method in a class.

(1) ORIGINAL


(2) TEST

(3) Error, when the Test calls "green.processStep()"


java.lang.StackOverflowError

at java.lang.reflect.Method.<init>(Method.java:116)
at java.lang.reflect.Method.copy(Method.java:143)
at java.lang.reflect.ReflectAccess.copyMethod(ReflectAccess.java:118)
at sun.reflect.ReflectionFactory.copyMethod(ReflectionFactory.java:
282)
at java.lang.Class.copyMethods(Class.java:2748)
at java.lang.Class.getDeclaredMethods(Class.java:1791)
at org.powermock.reflect.internal.WhiteboxImpl
$3.run(WhiteboxImpl.java:1608)
at org.powermock.reflect.internal.WhiteboxImpl
$3.run(WhiteboxImpl.java:1605)
at java.security.AccessController.doPrivileged(Native Method)
at
org.powermock.reflect.internal.WhiteboxImpl.getAllMethods(WhiteboxImpl.java:
1605)
at
org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:
1888)
at
org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:
1931)
at
org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate(WhiteboxImpl.java:
1025)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:89)

and a Part that has repeated itself...

at org.powermock.core.MockGateway.methodCall(MockGateway.java:168)
at
de.affinitas.billing.service.api.processor.AbstractPaymentOptionProcessor.equals(AbstractPaymentOptionProcessor.java)
at
org.mockito.internal.invocation.InvocationMatcher.matches(InvocationMatcher.java:
57)
at
org.mockito.internal.stubbing.InvocationContainerImpl.findAnswerFor(InvocationContainerImpl.java:
72)
at org.mockito.internal.MockHandler.handle(MockHandler.java:93)
at
org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:
291)
at
org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:
193)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)
at org.powermock.core.MockGateway.methodCall(MockGateway.java:168)
at
de.affinitas.billing.service.api.processor.AbstractPaymentOptionProcessor.equals(AbstractPaymentOptionProcessor.java)
at
org.mockito.internal.invocation.InvocationMatcher.matches(InvocationMatcher.java:
57)
at
org.mockito.internal.stubbing.InvocationContainerImpl.findAnswerFor(InvocationContainerImpl.java:
72)
at org.mockito.internal.MockHandler.handle(MockHandler.java:93)
at
org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:
291)
at
org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:
193)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)



What's the problem?

Thanks

michael

Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

Neither of your classes will compile as they are. How can you expect help if your examples are littered with compilation errors...

Sean


I love this place!
sam backet
Greenhorn

Joined: Sep 02, 2011
Posts: 22

it's difficult to reproduce the problem, because I'm working with many, complex classes.

Can you give me an idea, what can be wrong?

michael
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

sam backet wrote:it's difficult to reproduce the problem, because I'm working with many, complex classes.

Can you give me an idea, what can be wrong?

michael

The first step to solving a problem like this is often to try to find the simplest case that can reproduce it. Try removing code until the error goes away; then re-add the last thing. That gives a clue as to the space the problem is in and is something more concrete to ask about.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
sam backet
Greenhorn

Joined: Sep 02, 2011
Posts: 22

ok I have developed an example. The problem is the method "equal". If I delete it, it works and if I add it , the response is an error "StackOverFlow". But I need the method.












You can get it via mail.

Thanks

Sam
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

I don't understand your example, your GreenPayment class doesn't call the private authorise() method...?

Also don't do this:
Silently getting rid of exceptions is almost always bad, you should at least be printing out the exception, although in your case I'm not even sure why you have the try/catch there.

Finally I recommend you test that the private method is being called once at the end (as in your example it's not being called):
Sean
sam backet
Greenhorn

Joined: Sep 02, 2011
Posts: 22

the "autorise"call is not the problem. it throw the exception, when the program tries to call the "processStep"

and in that case.. I got the error, which I have written above. -> StackOverflow
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

Looks like your error is related to this: http://code.google.com/p/powermock/issues/detail?id=88.

As a note, if you remove the final keyword from your equals() method then your code runs fine.
Sean
sam backet
Greenhorn

Joined: Sep 02, 2011
Posts: 22

yes, it works, but why....?

thanks ..

sam
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

I'm not sure why, but I suspect the mock object framework is subclassing your object and "changing things."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PowerMock: StackOverFlow