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
RevLine 
[785]1package testcode;
2
3import static org.junit.Assert.assertEquals;
[1269]4import static org.junit.Assert.assertFalse;
[1017]5import static org.mockito.ArgumentMatchers.any;
[1279]6import static org.mockito.ArgumentMatchers.anyInt;
[1014]7import static org.mockito.ArgumentMatchers.eq;
[785]8import static org.mockito.Mockito.mock;
[1279]9import static org.mockito.Mockito.times;
10import static org.mockito.Mockito.verify;
[785]11import static org.mockito.Mockito.when;
12
[1279]13import org.eclipse.jdt.annotation.NonNull;
[785]14import org.junit.Test;
[1011]15import org.junit.internal.runners.JUnit4ClassRunner;
16import org.junit.runner.RunWith;
[1070]17import org.mockito.AdditionalMatchers;
[785]18
19class TestClass {
20 public int f(int x) {
21 return x + 1;
22 }
23}
24
[1017]25class NumberClass {
26 public int f(Number x) {
27 return x.intValue();
28 }
29}
30
[785]31/**
32 * Test for mockito translator. This program is going to be translated and run
33 * in python.
34 */
[1011]35@RunWith(JUnit4ClassRunner.class)
[785]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() {
[1279]47 final @NonNull TestClass test = mock(TestClass.class);
[785]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
[1014]54 @Test
55 public void testEqMatcher() {
[1279]56 final @NonNull TestClass test = mock(TestClass.class);
[1014]57 when(test.f(eq(3))).thenReturn(1);
[1015]58 when(test.f(eq(4))).thenReturn(2);
[1014]59 assertEquals(1, test.f(3));
[1015]60 assertEquals(2, test.f(4));
[1014]61 }
62
[1274]63 // does not work together with workaround for #396...
[1017]64 @Test
65 public void testAnyOfClass() {
[1279]66 final @NonNull NumberClass test = mock(NumberClass.class);
[1017]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
[1070]74 @Test
75 public void testNot() {
[1279]76 final @NonNull TestClass test = mock(TestClass.class);
[1070]77 when(test.f(AdditionalMatchers.not(eq(3)))).thenReturn(1);
78 assertEquals(1, test.f(1));
79 assertEquals(1, test.f(2));
80 }
[1269]81
82 @Test
83 public void testNotEqual() {
[1279]84 final @NonNull TestClass a = mock(TestClass.class);
85 final @NonNull TestClass b = mock(TestClass.class);
[1269]86 assertFalse(a.equals(b));
87 }
88
[1279]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
[785]100}
Note: See TracBrowser for help on using the repository browser.