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