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.assertNull;
9   import static org.junit.jupiter.api.Assertions.assertSame;
10  import static org.junit.jupiter.api.Assertions.assertTrue;
11  
12  import java.io.Serializable;
13  import java.util.Set;
14  
15  import mockit.integration.junit5.JMockitExtension;
16  
17  import org.junit.jupiter.api.Test;
18  import org.junit.jupiter.api.extension.ExtendWith;
19  
20  /**
21   * The Class TestedMethodsTest.
22   */
23  @ExtendWith(JMockitExtension.class)
24  class TestedMethodsTest {
25  
26      /**
27       * The Interface Dependency.
28       */
29      public interface Dependency {
30      }
31  
32      /**
33       * The Interface AnotherDependency.
34       */
35      public interface AnotherDependency {
36      }
37  
38      /**
39       * The Class DependencyImpl.
40       */
41      static class DependencyImpl implements Dependency {
42      }
43  
44      /**
45       * The Interface BaseDAO.
46       *
47       * @param <T>
48       *            the generic type
49       */
50      @SuppressWarnings("unused")
51      public interface BaseDAO<T extends Serializable> {
52      }
53  
54      /**
55       * The Interface ConcreteDAO.
56       */
57      public interface ConcreteDAO extends BaseDAO<String> {
58      }
59  
60      /**
61       * The Class DAOImpl.
62       */
63      static class DAOImpl implements ConcreteDAO {
64      }
65  
66      /**
67       * The Class TestedClass.
68       */
69      static class TestedClass {
70          /** The dependency. */
71          Dependency dependency;
72          /** The dao. */
73          ConcreteDAO dao;
74          /** The another dependency. */
75          AnotherDependency anotherDependency;
76          /** The set. */
77          Set<?> set;
78      }
79  
80      /**
81       * Resolve dependency interfaces.
82       *
83       * @param dependencyInterface
84       *            the dependency interface
85       *
86       * @return the class<? extends dependency>
87       */
88      @Tested
89      static Class<? extends Dependency> resolveDependencyInterfaces(Class<Dependency> dependencyInterface) {
90          assertSame(Dependency.class, dependencyInterface);
91          return DependencyImpl.class;
92      }
93  
94      /**
95       * Resolve DAO interfaces.
96       *
97       * @param daoInterface
98       *            the dao interface
99       *
100      * @return the class
101      */
102     @Tested
103     Class<?> resolveDAOInterfaces(Class<? extends BaseDAO<?>> daoInterface) {
104         assertSame(ConcreteDAO.class, daoInterface);
105         return DAOImpl.class;
106     }
107 
108     /**
109      * Resolve anything else.
110      *
111      * @param anyInterface
112      *            the any interface
113      *
114      * @return the class
115      */
116     @Tested
117     Class<?> resolveAnythingElse(Class<?> anyInterface) {
118         assertSame(AnotherDependency.class, anyInterface);
119         return null;
120     }
121 
122     /** The tested. */
123     @Tested(fullyInitialized = true)
124     TestedClass tested;
125 
126     /**
127      * Inject interface implementations from classes returned from tested methods.
128      */
129     @Test
130     void injectInterfaceImplementationsFromClassesReturnedFromTestedMethods() {
131         assertTrue(tested.dependency instanceof DependencyImpl);
132         assertTrue(tested.dao instanceof DAOImpl);
133         assertNull(tested.anotherDependency);
134         assertNull(tested.set);
135     }
136 
137     /**
138      * The Class DAO1.
139      */
140     static final class DAO1 {
141     }
142 
143     /**
144      * The Class DAO2.
145      */
146     static final class DAO2 {
147     }
148 
149     /**
150      * The Interface BaseService.
151      */
152     public interface BaseService {
153     }
154 
155     /**
156      * The Class BaseServiceImpl.
157      *
158      * @param <D>
159      *            the generic type
160      */
161     static class BaseServiceImpl<D> {
162         /** The dao. */
163         D dao;
164     }
165 
166     /**
167      * The Interface Service1.
168      */
169     public interface Service1 extends BaseService {
170     }
171 
172     /**
173      * The Interface Service2.
174      */
175     public interface Service2 extends BaseService {
176     }
177 
178     /**
179      * The Class ConcreteService1.
180      */
181     static final class ConcreteService1 extends BaseServiceImpl<DAO1> implements Service1 {
182     }
183 
184     /**
185      * The Class ConcreteService2.
186      */
187     static final class ConcreteService2 extends BaseServiceImpl<DAO2> implements Service2 {
188         /** The service 1. */
189         Service1 service1;
190     }
191 
192     /**
193      * Resolve service interfaces.
194      *
195      * @param baseServiceType
196      *            the base service type
197      *
198      * @return the class<? extends base service impl<?>>
199      */
200     @Tested
201     Class<? extends BaseServiceImpl<?>> resolveServiceInterfaces(Class<? extends BaseService> baseServiceType) {
202         if (baseServiceType == Service1.class) {
203             return ConcreteService1.class;
204         }
205         if (baseServiceType == Service2.class) {
206             return ConcreteService2.class;
207         }
208         return null;
209     }
210 
211     /**
212      * Creates the complex objects with generic dependencies.
213      *
214      * @param service2
215      *            the service 2
216      */
217     @Test
218     void createComplexObjectsWithGenericDependencies(@Tested(fullyInitialized = true) ConcreteService2 service2) {
219         assertTrue(service2.dao instanceof DAO2);
220         Service1 service1 = service2.service1;
221         assertTrue(service1 instanceof ConcreteService1);
222         assertTrue(((ConcreteService1) service1).dao instanceof DAO1);
223     }
224 }