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