aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java
diff options
context:
space:
mode:
authorIttay Stern <ittay.stern@att.com>2019-09-09 17:08:58 +0300
committerIttay Stern <ittay.stern@att.com>2019-09-09 17:11:13 +0300
commitb83152ff4a535c71e082f68c2ffe531e299d1da4 (patch)
tree0538beb384fc3c07e06c19251ce2c9e475e240d8 /vid-app-common/src/test/java
parentaf88d413093778f9102080e32ebbd3071c81f61d (diff)
initMockitoMocks: Generify null-placing
Issue-ID: VID-378 Change-Id: I6c2f91b7d54aee0e1327c50e8846bb86e820f796 Signed-off-by: Ittay Stern <ittay.stern@att.com>
Diffstat (limited to 'vid-app-common/src/test/java')
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java10
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java37
2 files changed, 36 insertions, 11 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java
index e5799bcf0..8c33e7f64 100644
--- a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java
@@ -41,7 +41,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.ws.rs.core.Response;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
import org.onap.vid.aai.AaiClientInterface;
import org.onap.vid.aai.ExceptionWithRequestInfo;
import org.onap.vid.aai.util.AAITreeConverter;
@@ -292,14 +291,7 @@ public class AAIServiceTreeIntegrativeTest {
@BeforeMethod
public void initMocks() {
- aaiClient = null;
- aaiGetVersionByInvariantIdResponse = null;
- sdcService = null;
- serviceModelInflator = null;
- exceptionWithRequestInfo = null;
-
- MockitoAnnotations.initMocks(this);
-
+ TestUtils.initMockitoMocks(this);
aaiTreeNodeBuilder = new AAITreeNodeBuilder(aaiClient);
}
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<Field> allMockitoFieldsOf(Object object) {
+ final Predicate<Field> hasMockAnnotation = field -> field.getAnnotation(Mock.class) != null;
+ final Predicate<Field> 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" }