1 package otherTests.testng; 2 3 import static org.testng.Assert.assertFalse; 4 import static org.testng.Assert.assertTrue; 5 6 import mockit.Expectations; 7 import mockit.FullVerifications; 8 9 import org.testng.annotations.AfterMethod; 10 import org.testng.annotations.BeforeMethod; 11 import org.testng.annotations.Test; 12 13 public final class DynamicMockingInBeforeMethodTest { 14 static final class MockedClass { 15 boolean doSomething(int i) { 16 return i > 0; 17 } 18 } 19 20 final MockedClass anInstance = new MockedClass(); 21 22 @BeforeMethod 23 public void recordExpectationsOnDynamicallyMockedClass() { 24 assertTrue(anInstance.doSomething(56)); 25 assertFalse(anInstance.doSomething(-56)); 26 27 new Expectations(anInstance) { 28 { 29 anInstance.doSomething(anyInt); 30 result = true; 31 minTimes = 0; 32 } 33 }; 34 } 35 36 @AfterMethod 37 public void verifyThatDynamicallyMockedClassIsStillMocked() { 38 new FullVerifications() { 39 { 40 anInstance.doSomething(anyInt); 41 times = 1; 42 } 43 }; 44 } 45 46 @Test 47 public void testSomething() { 48 assertTrue(anInstance.doSomething(-56)); 49 } 50 51 @Test 52 public void testSomethingElse() { 53 assertTrue(anInstance.doSomething(-129)); 54 } 55 }