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
9 * http://www.apache.org/licenses/LICENSE-2.0
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========================================================================
19 package org.onap.dcaegen2.collectors.datafile.integration.junit5.mockito;
21 import static org.mockito.Mockito.mock;
23 import java.lang.reflect.Parameter;
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;
35 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/27/18
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.
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();