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