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