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