2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.collectors.datafile.integration.junit5.mockito;
23 import static org.mockito.Mockito.mock;
25 import java.lang.reflect.Parameter;
27 import org.junit.jupiter.api.extension.ExtensionContext;
28 import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
29 import org.junit.jupiter.api.extension.ExtensionContext.Store;
30 import org.junit.jupiter.api.extension.ParameterContext;
31 import org.junit.jupiter.api.extension.ParameterResolver;
32 import org.junit.jupiter.api.extension.TestInstancePostProcessor;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
37 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/27/18
39 * {@code MockitoExtension } showcases the {@link TestInstancePostProcessor} and {@link ParameterResolver} extension
40 * APIs of JUnit 5 by providing dependency injection support at the field level and at the method parameter level
41 * viaMockito 2.x's {@link Mock @Mock} annotation.
43 public class MockitoExtension implements TestInstancePostProcessor, ParameterResolver {
46 public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
47 MockitoAnnotations.initMocks(testInstance);
51 public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
52 return parameterContext.getParameter().isAnnotationPresent(Mock.class);
56 public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
57 return getMock(parameterContext.getParameter(), extensionContext);
60 private Object getMock(Parameter parameter, ExtensionContext extensionContext) {
61 Class<?> mockType = parameter.getType();
62 Store mocks = extensionContext.getStore(Namespace.create(MockitoExtension.class, mockType));
63 String mockName = getMockName(parameter);
65 if (mockName != null) {
66 return mocks.getOrComputeIfAbsent(mockName, key -> mock(mockType, mockName));
68 return mocks.getOrComputeIfAbsent(mockType.getCanonicalName(), key -> mock(mockType));
72 private String getMockName(Parameter parameter) {
73 String explicitMockName = parameter.getAnnotation(Mock.class).name().trim();
74 if (!explicitMockName.isEmpty()) {
75 return explicitMockName;
76 } else if (parameter.isNamePresent()) {
77 return parameter.getName();