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 | @Test
|
---|
60 | public void testAnyOfClass() {
|
---|
61 | NumberClass test = mock(NumberClass.class);
|
---|
62 | when(test.f(any(Double.class))).thenReturn(1);
|
---|
63 | when(test.f(any(Integer.class))).thenReturn(2);
|
---|
64 | assertEquals(1, test.f(3d));
|
---|
65 | assertEquals(2, test.f(4));
|
---|
66 |
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Test
|
---|
70 | public void testNot() {
|
---|
71 | TestClass test = mock(TestClass.class);
|
---|
72 | when(test.f(AdditionalMatchers.not(eq(3)))).thenReturn(1);
|
---|
73 | assertEquals(1, test.f(1));
|
---|
74 | assertEquals(1, test.f(2));
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Test
|
---|
78 | public void testNotEqual() {
|
---|
79 | TestClass a = mock(TestClass.class);
|
---|
80 | TestClass b = mock(TestClass.class);
|
---|
81 | assertFalse(a.equals(b));
|
---|
82 | }
|
---|
83 |
|
---|
84 | }
|
---|