View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertTrue;
5   
6   import edu.umd.cs.findbugs.annotations.NonNull;
7   
8   import java.util.Date;
9   
10  import org.junit.jupiter.api.Test;
11  
12  /**
13   * The Class NaturalOrderingTest.
14   */
15  final class NaturalOrderingTest {
16  
17      /**
18       * Verify standard comparable behavior in mocked class.
19       *
20       * @param a
21       *            the a
22       * @param b
23       *            the b
24       */
25      @Test
26      void verifyStandardComparableBehaviorInMockedClass(@Mocked Date a, @Mocked Date b) {
27          // noinspection EqualsWithItself
28          assertEquals(0, a.compareTo(a));
29          // noinspection EqualsWithItself
30          assertEquals(0, b.compareTo(b));
31  
32          int aXb = a.compareTo(b);
33          assertTrue(aXb != 0);
34  
35          int bXa = b.compareTo(a);
36          assertTrue(bXa != 0);
37  
38          assertEquals(aXb, -bXa);
39      }
40  
41      /**
42       * The Class ComparableClass.
43       */
44      @SuppressWarnings("ComparableImplementedButEqualsNotOverridden")
45      static final class ComparableClass implements Comparable<String> {
46  
47          /** The value. */
48          final String value;
49  
50          /**
51           * Instantiates a new comparable class.
52           *
53           * @param value
54           *            the value
55           */
56          ComparableClass(String value) {
57              this.value = value;
58          }
59  
60          @Override
61          public int compareTo(@NonNull String s) {
62              return value.compareTo(s);
63          }
64      }
65  
66      /**
67       * Mock override of compare to method.
68       *
69       * @param a
70       *            the a
71       * @param b
72       *            the b
73       */
74      @Test
75      void mockOverrideOfCompareToMethod(@Mocked final ComparableClass a, @Mocked final ComparableClass b) {
76          new Expectations() {
77              {
78                  a.compareTo(null);
79                  result = 5;
80                  a.compareTo(anyString);
81                  result = 123;
82              }
83          };
84  
85          new Expectations() {
86              {
87                  b.compareTo("test");
88                  result = -50;
89              }
90          };
91  
92          assertEquals(5, a.compareTo(null));
93          assertEquals(123, a.compareTo("test"));
94          assertEquals(-50, b.compareTo("test"));
95      }
96  
97      /**
98       * Mock override of compare to method in JRE class.
99       *
100      * @param a
101      *            the a
102      * @param b
103      *            the b
104      */
105     @Test
106     void mockOverrideOfCompareToMethodInJREClass(@Mocked final Date a, @Mocked final Date b) {
107         new Expectations() {
108             {
109                 a.compareTo(b);
110                 result = 5;
111             }
112         };
113 
114         assertEquals(5, a.compareTo(b));
115         assertTrue(b.compareTo(a) != 0);
116 
117         new Verifications() {
118             {
119                 a.compareTo((Date) any);
120                 b.compareTo(a);
121             }
122         };
123     }
124 }