From b83152ff4a535c71e082f68c2ffe531e299d1da4 Mon Sep 17 00:00:00 2001 From: Ittay Stern Date: Mon, 9 Sep 2019 17:08:58 +0300 Subject: initMockitoMocks: Generify null-placing Issue-ID: VID-378 Change-Id: I6c2f91b7d54aee0e1327c50e8846bb86e820f796 Signed-off-by: Ittay Stern --- .../java/org/onap/vid/testUtils/TestUtils.java | 37 ++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java') diff --git a/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java index 399274d69..66052adeb 100644 --- a/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java +++ b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -46,11 +46,13 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; +import java.lang.reflect.Field; import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Iterator; import java.util.List; +import java.util.function.Predicate; import javax.ws.rs.client.Client; import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; @@ -60,6 +62,7 @@ import javax.ws.rs.core.Response; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.exception.ExceptionUtils; +import org.apache.commons.lang3.reflect.FieldUtils; import org.apache.commons.lang3.reflect.MethodUtils; import org.apache.commons.text.RandomStringGenerator; import org.apache.http.HttpResponseFactory; @@ -71,8 +74,11 @@ import org.apache.log4j.Logger; import org.json.JSONArray; import org.json.JSONObject; import org.junit.Assert; +import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.MockSettings; import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.mockito.stubbing.OngoingStubbing; @@ -176,6 +182,33 @@ public class TestUtils { .collect(toList()); } + private static List allMockitoFieldsOf(Object object) { + final Predicate hasMockAnnotation = field -> field.getAnnotation(Mock.class) != null; + final Predicate hasInjectMocksAnnotation = field -> field.getAnnotation(InjectMocks.class) != null; + + return Arrays.stream(FieldUtils.getAllFields(object.getClass())) + .filter(hasMockAnnotation.or(hasInjectMocksAnnotation)) + .collect(toList()); + } + + /** + * Calls MockitoAnnotations.initMocks after nullifying any field which is annotated @Mocke or @InjectMock. + * This makes a "hard rest" to any mocked state or instance. Expected to be invoked between any @Tests in class, by + * being called in TestNG's @BeforeMethod (or equivalently JUnit's @BeforeTest). + */ + public static void initMockitoMocks(Object testClass) { + for (Field field : allMockitoFieldsOf(testClass)) { + try { + // Write null to fields + FieldUtils.writeField(field, testClass, null, true); + } catch (ReflectiveOperationException e) { + ExceptionUtils.rethrow(e); + } + } + + MockitoAnnotations.initMocks(testClass); + } + /** * Sets each String property with a value equal to the name of * the property; e.g.: { name: "name", city: "city" } -- cgit 1.2.3-korg