View Javadoc
1   package otherTests.testng;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.testng.Assert.assertFalse;
5   import static org.testng.Assert.assertTrue;
6   
7   import mockit.Expectations;
8   import mockit.FullVerifications;
9   import mockit.Injectable;
10  import mockit.Tested;
11  import mockit.Verifications;
12  import mockit.VerificationsInOrder;
13  import mockit.integration.MockedClass;
14  
15  import org.testng.annotations.AfterMethod;
16  import org.testng.annotations.BeforeMethod;
17  import org.testng.annotations.Test;
18  
19  import otherTests.TestedClass;
20  
21  public final class TestNGExpectationsTest {
22      @Tested
23      TestedClass tested;
24      @Injectable
25      MockedClass dependency;
26      @Injectable
27      MockedClass mock2;
28  
29      @BeforeMethod
30      void setUpTestMethod1() {
31          new Expectations() {
32              {
33                  mock2.doSomethingElse(anyInt);
34                  result = true;
35                  minTimes = 0;
36              }
37          };
38      }
39  
40      @BeforeMethod
41      void setUpTestMethod2() {
42          new Expectations() {
43              {
44                  dependency.getValue();
45                  result = "mocked";
46                  minTimes = 0;
47              }
48          };
49      }
50  
51      @AfterMethod
52      public void tearDownTestMethod() {
53          new Verifications() {
54              {
55                  mock2.doSomethingElse(6);
56              }
57          };
58      }
59  
60      @Test
61      public void testSomething() {
62          new Expectations() {
63              {
64                  dependency.doSomething(anyInt);
65                  result = true;
66                  times = 2;
67              }
68          };
69  
70          assertTrue(dependency.doSomething(5));
71          assertEquals("mocked", dependency.getValue());
72          assertTrue(tested.doSomething(-5));
73          assertTrue(mock2.doSomethingElse(6));
74  
75          new FullVerifications(dependency) {
76              {
77                  dependency.getValue();
78              }
79          };
80      }
81  
82      @Test
83      public void testSomethingElse() {
84          assertEquals("mocked", dependency.getValue());
85          assertFalse(tested.doSomething(41));
86          assertTrue(mock2.doSomethingElse(6));
87  
88          new VerificationsInOrder() {
89              {
90                  dependency.getValue();
91                  dependency.doSomething(anyInt);
92              }
93          };
94      }
95  }