source: java2python/mockito-t/test/testcode/MockitoBasicTest.java@ 1282

Last change on this file since 1282 was 1279, checked in by wouter, 7 weeks ago

#399 bump version. added verify, anyInt support

File size: 2.6 KB
Line 
1package testcode;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.mockito.ArgumentMatchers.any;
6import static org.mockito.ArgumentMatchers.anyInt;
7import static org.mockito.ArgumentMatchers.eq;
8import static org.mockito.Mockito.mock;
9import static org.mockito.Mockito.times;
10import static org.mockito.Mockito.verify;
11import static org.mockito.Mockito.when;
12
13import org.eclipse.jdt.annotation.NonNull;
14import org.junit.Test;
15import org.junit.internal.runners.JUnit4ClassRunner;
16import org.junit.runner.RunWith;
17import org.mockito.AdditionalMatchers;
18
19class TestClass {
20 public int f(int x) {
21 return x + 1;
22 }
23}
24
25class NumberClass {
26 public int f(Number x) {
27 return x.intValue();
28 }
29}
30
31/**
32 * Test for mockito translator. This program is going to be translated and run
33 * in python.
34 */
35@RunWith(JUnit4ClassRunner.class)
36public class MockitoBasicTest {
37
38 /**
39 * NOTE NO CONSTRUCTOR. This is needed to check that the proper constructor,
40 * matching in this case the implicit superclass TestCase from Python, is
41 * created. This is a special case, in normal cases java already enforces
42 * all required code including the call to super().
43 */
44
45 @Test
46 public void testStub1() {
47 final @NonNull TestClass test = mock(TestClass.class);
48 when(test.f(3)).thenReturn(1);
49 // the mock did not do any when.thenReturn and should fail
50 assertEquals(1, test.f(3));
51
52 }
53
54 @Test
55 public void testEqMatcher() {
56 final @NonNull TestClass test = mock(TestClass.class);
57 when(test.f(eq(3))).thenReturn(1);
58 when(test.f(eq(4))).thenReturn(2);
59 assertEquals(1, test.f(3));
60 assertEquals(2, test.f(4));
61 }
62
63 // does not work together with workaround for #396...
64 @Test
65 public void testAnyOfClass() {
66 final @NonNull NumberClass test = mock(NumberClass.class);
67 when(test.f(any(Double.class))).thenReturn(1);
68 when(test.f(any(Integer.class))).thenReturn(2);
69 assertEquals(1, test.f(3d));
70 assertEquals(2, test.f(4));
71
72 }
73
74 @Test
75 public void testNot() {
76 final @NonNull TestClass test = mock(TestClass.class);
77 when(test.f(AdditionalMatchers.not(eq(3)))).thenReturn(1);
78 assertEquals(1, test.f(1));
79 assertEquals(1, test.f(2));
80 }
81
82 @Test
83 public void testNotEqual() {
84 final @NonNull TestClass a = mock(TestClass.class);
85 final @NonNull TestClass b = mock(TestClass.class);
86 assertFalse(a.equals(b));
87 }
88
89 @Test
90 public void testVerify() {
91 final @NonNull TestClass a = mock(TestClass.class);
92 when(a.f(anyInt())).thenReturn(1);
93 a.f(1);
94 a.f(1);
95 verify(a, times(2)).f(1);
96 verify(a, times(2)).f(any(Integer.class));
97 verify(a, times(0)).f(2);
98 }
99
100}
Note: See TracBrowser for help on using the repository browser.