easyforgood / mockito

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

Unable to use comparable matchers on subclass of class implementing Comparable #401

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
*What steps will reproduce the problem?*
1. Create a class implementing Comparable:
    public abstract class BaseComparable implements Comparable<BaseComparable> {

        private Integer value;

        public BaseComparable(Integer value) {
            this.value = value;
        }

        @Override
        public int compareTo(BaseComparable o) {
            return value.compareTo(o.value);
        }
    }

2. Create a subclass of the above class:
    public class ChildComparable extends BaseComparable {

        public ChildComparable(Integer value) {
            super(value);
        }
    }

3. Create a class/interface that uses the child class above:
    public interface ChildComparableUser {
        void someMethod(ChildComparable arg);
    }

4. Try to stub using one of the comparable methods in AdditionalMatchers class:
    @Test
    public void compare() {
        ChildComparableUser user = mock(ChildComparableUser.class);
        when(user.someMethod(geq(new ChildComparable(0)))).thenReturn("Positive");
    }

*What is the expected output?*

No problem with the above stubbing

*What do you see instead?*

The following compile failure:
"The method someMethod(MatchersTest.ChildComparable) in the type 
MatchersTest.ChildComparableUser is not applicable for the arguments 
(MatchersTest.BaseComparable)"

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

This problem is still present on the current (28 Nov 2012) trunk.

Please provide any additional information below.

This was found when trying to mock JodaTime LocalDate objects (which has a 
similar inheritance graph).

Original issue reported on code.google.com by ricen...@gmail.com on 28 Nov 2012 at 3:26

GoogleCodeExporter commented 8 years ago
You can find a fix (+ test) for the problem above in 
http://code.google.com/r/ricengel-mockito/

Original comment by ricen...@gmail.com on 28 Nov 2012 at 3:28

GoogleCodeExporter commented 8 years ago
Hi,

Interesting issue it seems. Thank you for the pull request. However as you did 
change some signatures, did you have the opportunity to confirm this change is 
compile time and runtime (binary) compatible with previous writen code / 
binaries ?

For the reader, here's the reference to the current specific changeset made by 
Ricardo Engel : 
http://code.google.com/r/ricengel-mockito/source/detail?r=e4e002e2949f492df681a7
80467239cf57be3f1d

Cheers,
Brice

Original comment by brice.du...@gmail.com on 2 Dec 2012 at 10:13

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 2 Dec 2012 at 10:16

GoogleCodeExporter commented 8 years ago
Hi Brice,

Sorry, just realised that I never answered to your question. In regards to the 
change, it should be fully compile time compatible as all tests were still 
untouched and passing after the code change. However, I don't this will be a 
binary compatible change as the runtime class has changed from Comparable to 
Object (thanks to type erasure).

Cheers,

Ricardo

Original comment by ricen...@gmail.com on 27 Aug 2013 at 6:11