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.testng.Assert.assertThrows;
9   
10  import java.util.IllegalFormatCodePointException;
11  
12  import mockit.Expectations;
13  import mockit.Mocked;
14  import mockit.integration.Collaborator;
15  
16  import org.testng.annotations.Test;
17  
18  // These tests are expected to fail, so they are kept inactive.
19  @Test(enabled = false)
20  public final class TestNGViolatedExpectationsTest {
21      @Test // fails with a "missing invocation" error
22      public void expectInvocationWhichDoesNotOccurInTestedCodeThatThrowsAnException_1(@Mocked final Collaborator mock) {
23          new Expectations() {
24              {
25                  mock.doSomething();
26              }
27          };
28      }
29  
30      @Test // fails with the exception thrown by tested code
31      public void expectInvocationWhichDoesNotOccurInTestedCodeThatThrowsAnException_2(@Mocked final Collaborator mock) {
32          new Expectations() {
33              {
34                  mock.doSomething();
35                  result = new IllegalFormatCodePointException('x');
36              }
37          };
38  
39          mock.doSomething();
40      }
41  
42      @Test // fails with an "unexpected invocation" error
43      public void expectInvocationWhichDoesNotOccurInTestedCodeThatThrowsAnException_3(@Mocked Collaborator mock) {
44          new Expectations() {
45              {
46                  new Collaborator();
47                  maxTimes = 1;
48              }
49          };
50  
51          new Collaborator();
52          new Collaborator();
53      }
54  
55      @Test
56      public void expectInvocationWhichDoesNotOccurInTestedCodeThatThrowsAnException_4(@Mocked final Collaborator mock) {
57          new Expectations() {
58              {
59                  mock.doSomething();
60                  result = new IllegalFormatCodePointException('x');
61                  minTimes = 2;
62              }
63          };
64  
65          // fails with a "missing invocation" error after the exception thrown by tested code
66          assertThrows(IllegalFormatCodePointException.class, () -> {
67              mock.doSomething();
68          });
69      }
70  
71      @Test
72      public void expectInvocationWhichDoesNotOccurInTestedCodeThatThrowsAnException_5(@Mocked final Collaborator mock) {
73          new Expectations() {
74              {
75                  mock.doSomething();
76                  result = new IllegalFormatCodePointException('x');
77              }
78          };
79  
80          // fails with a different exception than expected
81          assertThrows(AssertionError.class, () -> {
82              mock.doSomething();
83          });
84      }
85  
86      @Test
87      public void expectInvocationWhichDoesNotOccurInTestedCodeThatThrowsAnException_6(@Mocked final Collaborator mock) {
88          new Expectations() {
89              {
90                  // fails without the expected exception being thrown
91                  assertThrows(AssertionError.class, () -> {
92                      mock.doSomething();
93                  });
94                  result = new IllegalFormatCodePointException('x');
95              }
96          };
97      }
98  }