diff options
author | 2018-02-20 08:18:45 -0500 | |
---|---|---|
committer | 2018-02-21 14:04:32 +0000 | |
commit | 0b5e039860270c972f979ea6218e5ae1daaab53b (patch) | |
tree | e3e86b68a41afc319d73b16931ac3a8aebb8928c /appc-provider/appc-provider-bundle | |
parent | faae86dcfaab2d8ecc966540ab262bd10849fd01 (diff) |
migrate powermock to mockito appc provider
Change-Id: I15bd86d2d96ecf4070f1b6eafaf8b9ba9612b9cf
Issue-ID: APPC-647
Signed-off-by: Taka Cho <tc012c@att.com>
Diffstat (limited to 'appc-provider/appc-provider-bundle')
2 files changed, 87 insertions, 14 deletions
diff --git a/appc-provider/appc-provider-bundle/src/test/java/org/onap/appc/provider/AppcProviderTest.java b/appc-provider/appc-provider-bundle/src/test/java/org/onap/appc/provider/AppcProviderTest.java index 31598c9c9..4408657b3 100644 --- a/appc-provider/appc-provider-bundle/src/test/java/org/onap/appc/provider/AppcProviderTest.java +++ b/appc-provider/appc-provider-bundle/src/test/java/org/onap/appc/provider/AppcProviderTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP : APPC * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs * ============================================================================= @@ -30,6 +30,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; @@ -54,14 +55,11 @@ import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.config.payload.Confi import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.vnf.resource.VnfResource; import org.opendaylight.yangtools.yang.common.RpcResult; import org.onap.appc.provider.topology.TopologyService; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; - import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.lang.reflect.Field; +import org.onap.appc.provider.Whitebox; import static org.mockito.Matchers.any; import static org.mockito.Mockito.doReturn; @@ -70,8 +68,7 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -@RunWith(PowerMockRunner.class) -@PrepareForTest({AppcProvider.class}) +@RunWith(MockitoJUnitRunner.class) public class AppcProviderTest extends AbstractDataBrokerTest { @Mock @@ -86,6 +83,7 @@ public class AppcProviderTest extends AbstractDataBrokerTest { private AppcProvider provider; private DataBroker dataBroker; + /** * The @Before annotation is defined in the AbstractDataBrokerTest class. The method setupWithDataBroker is invoked * from inside the @Before method and is used to initialize the databroker with objects for a test runs. In our case @@ -104,19 +102,19 @@ public class AppcProviderTest extends AbstractDataBrokerTest { NotificationProviderService nps = mock(NotificationProviderService.class); RpcProviderRegistry registry = mock(RpcProviderRegistry.class); BindingAwareBroker.RpcRegistration rpcRegistration = mock(BindingAwareBroker.RpcRegistration.class); - PowerMockito.doReturn(rpcRegistration).when(registry).addRpcImplementation(any(), any()); + + doReturn(rpcRegistration).when(registry).addRpcImplementation(any(), any()); provider = spy(new AppcProvider(dataBroker, nps, registry)); - PowerMockito.doReturn(topologyService).when(provider).getTopologyService(); + doReturn(topologyService).when(provider).getTopologyService(); } @Test public void testConstructor() throws Exception { - ExecutorService executorService = Whitebox.getInternalState(provider, "executor"); + Object executorService = Whitebox.getInternalState(provider, "executor"); Assert.assertNotNull(executorService); - BindingAwareBroker.RpcRegistration internalRpcRegistration = Whitebox.getInternalState(provider, - "rpcRegistration"); + Object internalRpcRegistration = Whitebox.getInternalState(provider,"rpcRegistration"); Assert.assertNotNull(internalRpcRegistration); } @@ -139,7 +137,7 @@ public class AppcProviderTest extends AbstractDataBrokerTest { doReturn(configPayload).when(modifyConfigInput).getConfigPayload(); // mock output RpcResult<ModifyConfigOutput> modifyConfigOutput = mock(RpcResult.class); - PowerMockito.doReturn(modifyConfigOutput).when(topologyService).modifyConfig(any(), any()); + doReturn(modifyConfigOutput).when(topologyService).modifyConfig(any(), any()); Future<RpcResult<ModifyConfigOutput>> rpcResultFuture = provider.modifyConfig(modifyConfigInput); diff --git a/appc-provider/appc-provider-bundle/src/test/java/org/onap/appc/provider/Whitebox.java b/appc-provider/appc-provider-bundle/src/test/java/org/onap/appc/provider/Whitebox.java new file mode 100644 index 000000000..e065d1e7a --- /dev/null +++ b/appc-provider/appc-provider-bundle/src/test/java/org/onap/appc/provider/Whitebox.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (c) 2007 Mockito contributors + * ============================================================================= + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.provider; + +import java.lang.reflect.Field; + +public class Whitebox { + public static Object getInternalState(Object target, String field) { + Class<?> c = target.getClass(); + try { + Field f = getFieldFromHierarchy(c, field); + f.setAccessible(true); + return f.get(target); + } catch (Exception e) { + throw new RuntimeException("Unable to get internal state on a private field. Please report to mockito mailing list.", e); + } + } + + public static void setInternalState(Object target, String field, Object value) { + Class<?> c = target.getClass(); + try { + Field f = getFieldFromHierarchy(c, field); + f.setAccessible(true); + f.set(target, value); + } catch (Exception e) { + throw new RuntimeException("Unable to set internal state on a private field. Please report to mockito mailing list.", e); + } + } + + private static Field getFieldFromHierarchy(Class<?> clazz, String field) { + Field f = getField(clazz, field); + while (f == null && clazz != Object.class) { + clazz = clazz.getSuperclass(); + f = getField(clazz, field); + } + if (f == null) { + throw new RuntimeException( + "You want me to get this field: '" + field + + "' on this class: '" + clazz.getSimpleName() + + "' but this field is not declared withing hierarchy of this class!"); + } + return f; + } + + private static Field getField(Class<?> clazz, String field) { + try { + return clazz.getDeclaredField(field); + } catch (NoSuchFieldException e) { + return null; + } + } +} + |