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