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

Last change on this file since 1021 was 1017, checked in by wouter, 4 months ago

#344 added test showing any() needs also fixing

File size: 1.6 KB
Line 
1package testcode;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.ArgumentMatchers.any;
5import static org.mockito.ArgumentMatchers.eq;
6import static org.mockito.Mockito.mock;
7import static org.mockito.Mockito.when;
8
9import org.junit.Test;
10import org.junit.internal.runners.JUnit4ClassRunner;
11import org.junit.runner.RunWith;
12
13class TestClass {
14 public int f(int x) {
15 return x + 1;
16 }
17}
18
19class 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)
30public 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}
Note: See TracBrowser for help on using the repository browser.