joepadde / mockito

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

UnfinishedStubbingException with JDK 7 and SortedSets #468

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

1. With JDK 6 it was possible to create a TreeSet of mocked elements inline 
within the 'thenReturn' call e.g. such a test was possible:

    interface MySortedDateSet {
        SortedSet<Date> getDates();
    }

    @Test
    public void testSortedSet() {
        final Date firstDate = mock(Date.class);
        final Date secondDate = mock(Date.class);

        final MySortedDateSet dateSet = mock(MySortedDateSet.class);

        when(dateSet.getDates()).thenReturn(new TreeSet<>(Arrays.asList(firstDate, secondDate)));
    }

2. With JDK 7 the API of the TreeSet has been changed (see 'Synopsis: Inserting 
an Invalid Element into a TreeMap Throws an NPE' in 
http://www.oracle.com/technetwork/java/javase/compatibility-417013.html)

3. Now with JDK 7 the test above fails with an 'UnfinishedStubbingException'. 
Details:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at mypackage.TreeSetTest.testSortedSet(TreeSetTest.java:39)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, you naughty developer!

    at java.util.Date.compareTo(Date.java:129)
    at java.util.TreeMap.compare(TreeMap.java:1188)
    at java.util.TreeMap.put(TreeMap.java:531)
    at java.util.TreeSet.add(TreeSet.java:255)
    at java.util.AbstractCollection.addAll(AbstractCollection.java:342)
    at java.util.TreeSet.addAll(TreeSet.java:312)
    at java.util.TreeSet.<init>(TreeSet.java:160)
    at mypackage.TreeSetTest.testSortedSet(TreeSetTest.java:39)
    ...

What is the expected output? What do you see instead?

    Since the stubbing has been finsihed and there is no inline mock created within the 'thenReturn' call, the UnfinishedStubbingException shouldn't be thrown. 

What version of the product are you using? On what operating system?
    jdk1.7.0_45

Please provide any additional information below.

    The test will succeed if the TreeSet will be created outside the 'thenReturn' call:

    interface MySortedDateSet {
        SortedSet<Date> getDates();
    }

    @Test
    public void testSortedSet() {
        final Date firstDate = mock(Date.class);
        final Date secondDate = mock(Date.class);

        final MySortedDateSet dateSet = mock(MySortedDateSet.class);

        final TreeSet<Date> dateTreeSet = new TreeSet<>(Arrays.asList(firstDate, secondDate));
        when(dateSet.getDates()).thenReturn(dateTreeSet);
    }

Original issue reported on code.google.com by micta...@googlemail.com on 15 Jan 2014 at 12:10

GoogleCodeExporter commented 8 years ago
Hey,

Thanks for reporting. Do you mind taking a stab at fixing this bug and 
submitting back a pull request?

Original comment by szcze...@gmail.com on 19 Jan 2014 at 10:57