View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package integration.tests;
7   
8   import org.junit.jupiter.api.MethodOrderer;
9   import org.junit.jupiter.api.Test;
10  import org.junit.jupiter.api.TestMethodOrder;
11  
12  @TestMethodOrder(MethodOrderer.MethodName.class)
13  class UnreachableStatementsTest extends CoverageTest {
14      UnreachableStatements tested;
15  
16      @Test
17      void staticClassInitializerShouldHaveNoBranches() {
18          assertLine(8, 0, 0, 0); // one execution for each test (the constructor), plus one for the static initializer
19      }
20  
21      @Test
22      void nonBranchingMethodWithUnreachableLines() {
23          try {
24              tested.nonBranchingMethodWithUnreachableLines();
25          } catch (AssertionError ignore) {
26          }
27  
28          assertLines(17, 20, 2);
29          assertLine(17, 1, 1, 1);
30          assertLine(18, 1, 1, 1);
31          assertLine(19, 1, 0, 0);
32          assertLine(20, 1, 0, 0);
33      }
34  
35      @Test
36      void branchingMethodWithUnreachableLines_avoidAssertion() {
37          tested.branchingMethodWithUnreachableLines(0);
38  
39          assertLines(29, 35, 3);
40          assertLine(29, 3, 2, 1);
41          assertLine(30, 1, 0, 0);
42          assertLine(31, 1, 0, 0);
43          assertLine(34, 1, 1, 1);
44          assertLine(35, 1, 1, 1);
45      }
46  
47      @Test
48      void branchingMethodWithUnreachableLines_hitAndFailAssertion() {
49          try {
50              tested.branchingMethodWithUnreachableLines(1);
51          } catch (AssertionError ignore) {
52          }
53  
54          // Accounts for executions from previous test.
55          assertLines(29, 35, 4);
56          assertLine(29, 3, 3, 2);
57          assertLine(30, 1, 1, 1);
58          assertLine(31, 1, 0, 0);
59      }
60  }