1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockitousage.bugs;
7 
8 import org.junit.Test;
9 import org.mockitoutil.TestBase;
10 
11 import java.util.Date;
12 import java.util.Set;
13 import java.util.TreeSet;
14 
15 import static org.junit.Assert.assertEquals;
16 import static org.mockito.Mockito.*;
17 
18 //see issue 184
19 public class ShouldMocksCompareToBeConsistentWithEqualsTest extends TestBase {
20 
21     @Test
should_compare_to_be_consistent_with_equals()22     public void should_compare_to_be_consistent_with_equals() {
23         //given
24         Date today    = mock(Date.class);
25         Date tomorrow = mock(Date.class);
26 
27         //when
28         Set<Date> set = new TreeSet<Date>();
29         set.add(today);
30         set.add(tomorrow);
31 
32         //then
33         assertEquals(2, set.size());
34     }
35 
36     @Test
should_compare_to_be_consistent_with_equals_when_comparing_the_same_reference()37     public void should_compare_to_be_consistent_with_equals_when_comparing_the_same_reference() {
38         //given
39         Date today    = mock(Date.class);
40 
41         //when
42         Set<Date> set = new TreeSet<Date>();
43         set.add(today);
44         set.add(today);
45 
46         //then
47         assertEquals(1, set.size());
48     }
49 
50     @Test
should_allow_stubbing_and_verifying_compare_to()51     public void should_allow_stubbing_and_verifying_compare_to() {
52         //given
53         Date mock    = mock(Date.class);
54         when(mock.compareTo(any(Date.class))).thenReturn(10);
55 
56         //when
57         mock.compareTo(new Date());
58 
59         //then
60         assertEquals(10, mock.compareTo(new Date()));
61         verify(mock, atLeastOnce()).compareTo(any(Date.class));
62     }
63 
64     @Test
should_reset_not_remove_default_stubbing()65     public void should_reset_not_remove_default_stubbing() {
66         //given
67         Date mock    = mock(Date.class);
68         reset(mock);
69 
70         //then
71         assertEquals(1, mock.compareTo(new Date()));
72     }
73 }
74