mlinhard / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

OngoingStubbing tries to stub wrong method #288

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. The following code for tree testing throws an exception:

    private void createRelationAsParentTreeTest(Node parent, List<Node> children) {
        List<Node> childrenList = new ArrayList<Node>();
        OngoingStubbing<List<Node>> parentChildrenList = when(parent.getChildren()).thenReturn(new ArrayList<Node>(childrenList));
        for (IEmail child : children) {
            when(child.getParent()).thenReturn(parent);
            when(parent.isAncestorOf(child)).thenReturn(true);
            childrenList.add(child);
            parentChildrenList.thenReturn(new ArrayList<Node>(childrenList));
        }
    }
2.  The initialization of OngoingStubbing Throws an exception when trying to 
initialize with when(parent.getChildren()) only.

What is the expected output? What do you see instead?
I've expected it to make sure that consequtive calls of getChildren will return 
the list of children, each time with a new child.

I got the following exception:
"""
org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
ArrayList cannot be returned by isAncestorOf()
isAncestorOf() should return boolean
"""

What version of the product are you using? On what operating system?
Windows 7, Spring source, Mockito 1.8.5

Please provide any additional information below.

Original issue reported on code.google.com by ido.ba...@gmail.com on 2 Nov 2011 at 9:32

GoogleCodeExporter commented 9 years ago
With this code you completely confused Mockito. You should *never* have a 
reference on OngoingStubbing. 
In order to work consecutive stubbing must be done consecutively, this how the 
API works. In this snippet you are stubbing here and there.

You should first prepare the list of your returned result, in another method if 
you see fit, then stub with an answer that will use these results.

Please ask first the mailing list in cases like this, so we can discuss a bit 
before reporting an issue.

Original comment by brice.du...@gmail.com on 2 Nov 2011 at 10:19