package testcode; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import org.junit.Test; import org.junit.internal.runners.JUnit4ClassRunner; import org.junit.runner.RunWith; class TestClass { public int f(int x) { return x + 1; } } /** * Test for mockito translator. This program is going to be translated and run * in python. */ @RunWith(JUnit4ClassRunner.class) public class MockitoBasicTest { /** * NOTE NO CONSTRUCTOR. This is needed to check that the proper constructor, * matching in this case the implicit superclass TestCase from Python, is * created. This is a special case, in normal cases java already enforces * all required code including the call to super(). */ @Test public void testStub1() { TestClass test = mock(TestClass.class); when(test.f(3)).thenReturn(1); // the mock did not do any when.thenReturn and should fail assertEquals(1, test.f(3)); } @Test public void testEqMatcher() { TestClass test = mock(TestClass.class); when(test.f(eq(3))).thenReturn(1); assertEquals(1, test.f(3)); } }