View Javadoc
1   package otherTests;
2   
3   import static org.hamcrest.CoreMatchers.is;
4   import static org.junit.jupiter.api.Assertions.assertEquals;
5   import static org.junit.jupiter.api.Assertions.assertFalse;
6   import static org.junit.jupiter.api.Assertions.assertNotNull;
7   import static org.junit.jupiter.api.Assertions.assertTrue;
8   
9   import java.util.ArrayList;
10  import java.util.List;
11  
12  import mockit.ClassWithObjectOverrides;
13  import mockit.Delegate;
14  import mockit.Expectations;
15  import mockit.Injectable;
16  import mockit.Invocation;
17  import mockit.Mock;
18  import mockit.Mocked;
19  import mockit.Verifications;
20  
21  import org.junit.Test;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  /**
26   * The Class JUnit4Test.
27   */
28  public final class JUnit4Test {
29  
30      /** The logger. */
31      private static final Logger logger = LoggerFactory.getLogger(JUnit4Test.class);
32  
33      /** The mock. */
34      @Mocked
35      ClassWithObjectOverrides mock;
36  
37      /**
38       * Use mocked instance.
39       */
40      @SuppressWarnings("EqualsBetweenInconvertibleTypes")
41      @Test
42      public void useMockedInstance() {
43          new ClassWithObjectOverrides("test");
44          assertFalse(mock.toString().isEmpty());
45          mock.equals("123");
46          // noinspection ObjectEqualsNull
47          mock.equals(null);
48  
49          new Verifications() {
50              {
51                  String s;
52                  mock.equals(s = withCapture());
53                  assertEquals("123", s);
54  
55                  List<ClassWithObjectOverrides> objs = withCapture(new ClassWithObjectOverrides("test"));
56                  assertEquals(1, objs.size());
57  
58                  mock.equals(withNull());
59              }
60          };
61      }
62  
63      /**
64       * The Class AnotherClass.
65       */
66      static class AnotherClass {
67  
68          /**
69           * Do something.
70           *
71           * @param i
72           *            the i
73           * @param l
74           *            the l
75           * @param s
76           *            the s
77           * @param b
78           *            the b
79           * @param c
80           *            the c
81           * @param d
82           *            the d
83           * @param f
84           *            the f
85           * @param str
86           *            the str
87           */
88          void doSomething(int i, long l, Short s, byte b, char c, double d, float f, String str) {
89              logger.info("{}", i + l + s + b + d + f);
90              logger.info("{}", c + str);
91          }
92  
93          /**
94           * Gets the modified value.
95           *
96           * @param s
97           *            the s
98           *
99           * @return the modified value
100          */
101         String getModifiedValue(String s) {
102             return s.toLowerCase();
103         }
104     }
105 
106     /**
107      * Use argument matching fields.
108      *
109      * @param anotherMock
110      *            the another mock
111      */
112     @Test
113     public void useArgumentMatchingFields(@Injectable final AnotherClass anotherMock) {
114         new Expectations() {
115             {
116                 anotherMock.doSomething(anyInt, anyLong, anyShort, anyByte, anyChar, anyDouble, anyFloat, anyString);
117             }
118         };
119 
120         anotherMock.doSomething(1, 2, (short) 3, (byte) 4, 'c', 1.2, 2.5F, "");
121     }
122 
123     /**
124      * Use argument matching methods.
125      *
126      * @param anotherMock
127      *            the another mock
128      */
129     @Test
130     public void useArgumentMatchingMethods(@Injectable final AnotherClass anotherMock) {
131         new Expectations() {
132             {
133                 anotherMock.doSomething(withAny(0), withEqual(2L), withInstanceOf(short.class), withNotEqual((byte) 1),
134                         withInstanceLike(' '), withEqual(1.2, 0), withEqual(2.5F, 0), withSameInstance("test"));
135 
136                 anotherMock.getModifiedValue(withSubstring("abc"));
137                 result = "Abc";
138                 anotherMock.getModifiedValue(withPrefix("TX"));
139                 result = "abc";
140                 anotherMock.getModifiedValue(withSuffix("X"));
141                 result = "ABC";
142                 anotherMock.getModifiedValue(withMatch("\\d+"));
143                 result = "number";
144                 anotherMock.getModifiedValue(withArgThat(is("test")));
145                 result = "test";
146 
147                 anotherMock.getModifiedValue("Delegate");
148                 result = new Delegate<Object>() {
149                     @Mock
150                     String delegate(Invocation inv, String s) {
151                         assertNotNull(inv.getInvokedMember());
152                         assertTrue(inv.getInvocationIndex() >= 0);
153                         assertTrue(inv.getInvocationCount() >= 1);
154                         assertEquals(1, inv.getInvokedArguments().length);
155                         return inv.proceed();
156                     }
157                 };
158             }
159         };
160 
161         anotherMock.doSomething(1, 2, (short) 3, (byte) 4, 'c', 1.2, 2.5F, "test");
162 
163         assertEquals("Abc", anotherMock.getModifiedValue("test abc xyz"));
164         assertEquals("abc", anotherMock.getModifiedValue("TX test"));
165         assertEquals("ABC", anotherMock.getModifiedValue("test X"));
166         assertEquals("number", anotherMock.getModifiedValue("123"));
167         assertEquals("test", anotherMock.getModifiedValue("test"));
168         assertEquals("delegate", anotherMock.getModifiedValue("Delegate"));
169 
170         new Verifications() {
171             {
172                 List<String> values = new ArrayList<>();
173                 anotherMock.getModifiedValue(withCapture(values));
174                 assertEquals(6, values.size());
175             }
176         };
177     }
178 }