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.services.prh.integration.junit5.mockito;
23 import static org.mockito.Mockito.mock;
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;
36 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/27/18
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.
42 public class MockitoExtension implements TestInstancePostProcessor, ParameterResolver {
45 public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
46 MockitoAnnotations.initMocks(testInstance);
50 public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
51 return parameterContext.getParameter().isAnnotationPresent(Mock.class);
55 public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
56 return getMock(parameterContext.getParameter(), extensionContext);
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);
64 if (mockName != null) {
65 return mocks.getOrComputeIfAbsent(mockName, key -> mock(mockType, mockName));
67 return mocks.getOrComputeIfAbsent(mockType.getCanonicalName(), key -> mock(mockType));
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();