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

Last change on this file was 1269, checked in by wouter, 36 hours ago

#396 added test showing issue with mockito

File size: 2.0 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;
[1014]6import static org.mockito.ArgumentMatchers.eq;
[785]7import static org.mockito.Mockito.mock;
8import static org.mockito.Mockito.when;
9
10import org.junit.Test;
[1011]11import org.junit.internal.runners.JUnit4ClassRunner;
12import org.junit.runner.RunWith;
[1070]13import org.mockito.AdditionalMatchers;
[785]14
15class TestClass {
16 public int f(int x) {
17 return x + 1;
18 }
19}
20
[1017]21class NumberClass {
22 public int f(Number x) {
23 return x.intValue();
24 }
25}
26
[785]27/**
28 * Test for mockito translator. This program is going to be translated and run
29 * in python.
30 */
[1011]31@RunWith(JUnit4ClassRunner.class)
[785]32public 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
[1014]50 @Test
51 public void testEqMatcher() {
52 TestClass test = mock(TestClass.class);
53 when(test.f(eq(3))).thenReturn(1);
[1015]54 when(test.f(eq(4))).thenReturn(2);
[1014]55 assertEquals(1, test.f(3));
[1015]56 assertEquals(2, test.f(4));
[1014]57 }
58
[1017]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
[1070]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 }
[1269]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
[785]84}
Note: See TracBrowser for help on using the repository browser.