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