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.assertNotNull;
10  
11  import mockit.integration.junit5.JMockitExtension;
12  
13  import org.junit.jupiter.api.Test;
14  import org.junit.jupiter.api.extension.ExtendWith;
15  
16  /**
17   * The Class TestedClassWithNoPublicConstructorTest.
18   */
19  @ExtendWith(JMockitExtension.class)
20  class TestedClassWithNoPublicConstructorTest {
21  
22      /**
23       * The Class TestedClassWithPackagePrivateConstructor.
24       */
25      @SuppressWarnings("unused")
26      public static final class TestedClassWithPackagePrivateConstructor {
27  
28          /**
29           * Instantiates a new tested class with package private constructor.
30           *
31           * @param values
32           *            the values
33           */
34          private TestedClassWithPackagePrivateConstructor(int... values) {
35              throw new RuntimeException("Must not occur");
36          }
37  
38          /**
39           * Instantiates a new tested class with package private constructor.
40           *
41           * @param i
42           *            the i
43           * @param collaborator
44           *            the collaborator
45           */
46          TestedClassWithPackagePrivateConstructor(int i, Collaborator collaborator) {
47              assertEquals(123, i);
48              assertNotNull(collaborator);
49          }
50  
51          /**
52           * Instantiates a new tested class with package private constructor.
53           *
54           * @param i
55           *            the i
56           * @param collaborator
57           *            the collaborator
58           * @param s
59           *            the s
60           */
61          private TestedClassWithPackagePrivateConstructor(int i, Collaborator collaborator, String s) {
62              throw new RuntimeException("Must not occur");
63          }
64      }
65  
66      /**
67       * The Class TestedClassWithPrivateConstructor.
68       */
69      @SuppressWarnings("UnusedDeclaration")
70      static class TestedClassWithPrivateConstructor {
71  
72          /**
73           * Instantiates a new tested class with private constructor.
74           */
75          private TestedClassWithPrivateConstructor() {
76              throw new RuntimeException("Must not occur");
77          }
78  
79          /**
80           * Instantiates a new tested class with private constructor.
81           *
82           * @param collaborator
83           *            the collaborator
84           */
85          private TestedClassWithPrivateConstructor(Collaborator collaborator) {
86              assertNotNull(collaborator);
87          }
88      }
89  
90      /**
91       * The Class Collaborator.
92       */
93      static class Collaborator {
94          /**
95           * Do something.
96           */
97          static void doSomething() {
98          }
99      }
100 
101     /** The tested 1. */
102     @Tested
103     TestedClassWithPackagePrivateConstructor tested1;
104 
105     /** The tested 2. */
106     @Tested
107     TestedClassWithPrivateConstructor tested2;
108 
109     /** The i. */
110     @Injectable
111     int i = 123;
112 
113     /** The collaborator. */
114     @Injectable
115     Collaborator collaborator;
116 
117     /**
118      * Verify instantiation of tested objects through injected constructors.
119      */
120     @Test
121     void verifyInstantiationOfTestedObjectsThroughInjectedConstructors() {
122         assertNotNull(tested1);
123         assertNotNull(tested2);
124     }
125 }