af9c2b441c25d0e9893fe89b31ee7792afe0370e
[dcaegen2/services/prh.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.services.prh.integration.junit5.mockito;
22
23 import static org.mockito.Mockito.mock;
24
25 import java.lang.reflect.Parameter;
26 import org.junit.jupiter.api.extension.ExtensionContext;
27 import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
28 import org.junit.jupiter.api.extension.ExtensionContext.Store;
29 import org.junit.jupiter.api.extension.ParameterContext;
30 import org.junit.jupiter.api.extension.ParameterResolver;
31 import org.junit.jupiter.api.extension.TestInstancePostProcessor;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34
35 /**
36  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/27/18
37  *
38  *      {@code MockitoExtension } showcases the {@link TestInstancePostProcessor} and {@link ParameterResolver}
39  *      extension APIs of JUnit 5 by providing dependency injection support at the field level and at the method
40  *      parameter level viaMockito 2.x's {@link Mock @Mock} annotation.
41  */
42 public class MockitoExtension implements TestInstancePostProcessor, ParameterResolver {
43
44     @Override
45     public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
46         MockitoAnnotations.initMocks(testInstance);
47     }
48
49     @Override
50     public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
51         return parameterContext.getParameter().isAnnotationPresent(Mock.class);
52     }
53
54     @Override
55     public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
56         return getMock(parameterContext.getParameter(), extensionContext);
57     }
58
59     private Object getMock(Parameter parameter, ExtensionContext extensionContext) {
60         Class<?> mockType = parameter.getType();
61         Store mocks = extensionContext.getStore(Namespace.create(MockitoExtension.class, mockType));
62         String mockName = getMockName(parameter);
63
64         if (mockName != null) {
65             return mocks.getOrComputeIfAbsent(mockName, key -> mock(mockType, mockName));
66         } else {
67             return mocks.getOrComputeIfAbsent(mockType.getCanonicalName(), key -> mock(mockType));
68         }
69     }
70
71     private String getMockName(Parameter parameter) {
72         String explicitMockName = parameter.getAnnotation(Mock.class).name().trim();
73         if (!explicitMockName.isEmpty()) {
74             return explicitMockName;
75         } else if (parameter.isNamePresent()) {
76             return parameter.getName();
77         }
78         return null;
79     }
80
81
82 }