View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package mockit;
7   
8   import static org.junit.jupiter.api.Assertions.assertEquals;
9   import static org.junit.jupiter.api.Assertions.assertTrue;
10  
11  import java.util.Arrays;
12  import java.util.List;
13  
14  import mockit.integration.MockedClass;
15  
16  import org.junit.Test;
17  import org.junit.runner.RunWith;
18  import org.junit.runners.Parameterized;
19  import org.junit.runners.Parameterized.Parameters;
20  
21  import otherTests.TestedClass;
22  
23  /**
24   * The Class JUnit4ParametersTest.
25   */
26  @RunWith(Parameterized.class)
27  public final class JUnit4ParametersTest {
28  
29      /**
30       * Parameters.
31       *
32       * @return the list
33       */
34      @Parameters(name = "Input squared: {0} -> {1}")
35      public static List<Integer[]> parameters() {
36          Integer[][] data = { { 1, 1 }, { 2, 4 }, { 3, 9 } };
37          return Arrays.asList(data);
38      }
39  
40      /** The input. */
41      final int input;
42  
43      /** The expected. */
44      final int expected;
45  
46      /** The cut. */
47      @Tested
48      TestedClass cut;
49  
50      /** The dependency. */
51      @Injectable
52      MockedClass dependency;
53  
54      /**
55       * Instantiates a new j unit 4 parameters test.
56       *
57       * @param input
58       *            the input
59       * @param expected
60       *            the expected
61       */
62      public JUnit4ParametersTest(int input, int expected) {
63          this.input = input;
64          this.expected = expected;
65      }
66  
67      /**
68       * Use parameters.
69       *
70       * @param mock
71       *            the mock
72       */
73      @Test
74      public void useParameters(@Mocked final Runnable mock) {
75          new Expectations() {
76              {
77                  dependency.doSomething(anyInt);
78                  result = true;
79              }
80          };
81  
82          mock.run();
83          boolean didSomething = cut.doSomething(input);
84  
85          assertTrue(didSomething);
86  
87          int result = input * input;
88          assertEquals(expected, result);
89  
90          new Verifications() {
91              {
92                  mock.run();
93                  times = 1;
94              }
95          };
96      }
97  }