[785] | 1 | package testcode;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
[1269] | 4 | import static org.junit.Assert.assertFalse;
|
---|
[1017] | 5 | import static org.mockito.ArgumentMatchers.any;
|
---|
[1014] | 6 | import static org.mockito.ArgumentMatchers.eq;
|
---|
[785] | 7 | import static org.mockito.Mockito.mock;
|
---|
| 8 | import static org.mockito.Mockito.when;
|
---|
| 9 |
|
---|
| 10 | import org.junit.Test;
|
---|
[1011] | 11 | import org.junit.internal.runners.JUnit4ClassRunner;
|
---|
| 12 | import org.junit.runner.RunWith;
|
---|
[1070] | 13 | import org.mockito.AdditionalMatchers;
|
---|
[785] | 14 |
|
---|
| 15 | class TestClass {
|
---|
| 16 | public int f(int x) {
|
---|
| 17 | return x + 1;
|
---|
| 18 | }
|
---|
| 19 | }
|
---|
| 20 |
|
---|
[1017] | 21 | class 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] | 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 |
|
---|
[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 | }
|
---|