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