bc4e6401e971e1e3446c6623d9328c4aeefd0113
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END========================================================================
17  */
18
19 package org.onap.dcaegen2.collectors.datafile.integration.junit5.mockito;
20
21 import static org.mockito.Mockito.mock;
22
23 import java.lang.reflect.Parameter;
24
25 import org.junit.jupiter.api.extension.ExtensionContext;
26 import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
27 import org.junit.jupiter.api.extension.ExtensionContext.Store;
28 import org.junit.jupiter.api.extension.ParameterContext;
29 import org.junit.jupiter.api.extension.ParameterResolver;
30 import org.junit.jupiter.api.extension.TestInstancePostProcessor;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33
34 /**
35  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/27/18
36  *
37  *         {@code MockitoExtension} showcases the {@link TestInstancePostProcessor} and
38  *         {@link ParameterResolver} extension APIs of JUnit 5 by providing dependency injection
39  *         support at the field level and at the method parameter level via Mockito 2.x's
40  *         {@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 }