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