I am trying to write a Junit for the below java method in AEM
public final Resource getConfigProp(final Page page, final ConfigType configType, final ResourceResolver resolver) { final HierarchyNodeInheritanceValueMap iProp= new HierarchyNodeInheritanceValueMap( page.getContentResource()); iPropString[] services = inheritedPageProperties.getInherited(configType.PN_CONFIGURATIONS, new String[0]); final com.day.cq.wcm.webservicesupport.ConfigurationManager configurationManager = resolver .adaptTo(com.day.cq.wcm.webservicesupport.ConfigurationManager.class); if (null != configurationManager) { final Configuration cg = configurationManager.getConfiguration("tok-yo", services);// getConfiguration is coming as null in Mockito return cg.getContentResource(); } return null; }
Here is my Mockito. Everything is mocked correctly but for some reason getConfiguration method is giving me null. Can anyone tell why this mocking is not working?
@Before public void setUp() throws Exception { configMgr = mock(ConfigurationManager.class); config = mock(Configuration.class); currentPage = mock(Page.class); resource = mock(Resource.class); resourceResolver = mock(ResourceResolver.class); iValueMap = mock(HierarchyNodeInheritanceValueMap.class); configService = new ConfigImpl(); when(currentPage.getContentResource()).thenReturn(resource); whenNew(HierarchyNodeInheritanceValueMap.class).withArguments(resource) .thenReturn(iValueMap); when(iValueMap.getInherited("cq:cloudserviceconfigs", new String[0])) .thenReturn(TEST_SERVICES); when(configMgr.getConfiguration(TEST_CONFIG_NAME, TEST_SERVICES)).thenReturn(config); //getConfiguration is returning null when(config.getContentResource()).thenReturn(resource); } @Test public void testConfigProp() throws Exception { ValueMap valueMap = mock(ValueMap.class); when(resource.adaptTo(ValueMap.class)).thenReturn(valueMap); when(resourceResolver.adaptTo(ConfigurationManager.class)).thenReturn(configMgr); // Verify assertEquals(valueMap, configService.getConfigProp(currentPage, configType, resourceResolver)); }
Error trace:-
java.lang.NullPointerException
at com.aem.services.impl.ConfigImpl.getConfigResource(ConfigImpl.java:55)
at com.config.impl.ConfigImplTest.getConfigProp(ConfigImplTest.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Solved! Go to Solution.
Views
Replies
Total Likes
The parameters passing should be wrong.
Can you try with hardcoded values here:
when(configMgr.getConfiguration(TEST_CONFIG_NAME, TEST_SERVICES))
====
@Mock
private ConfigurationManager configMgr;
@Mock
private Configuration configuration;
in method===
testMethod() {
String[] configs = new String[]{"config1"};
when(configMgr.getConfiguration("yourValue", configs)).thenReturn(configuration);
}
The parameters passing should be wrong.
Can you try with hardcoded values here:
when(configMgr.getConfiguration(TEST_CONFIG_NAME, TEST_SERVICES))
====
@Mock
private ConfigurationManager configMgr;
@Mock
private Configuration configuration;
in method===
testMethod() {
String[] configs = new String[]{"config1"};
when(configMgr.getConfiguration("yourValue", configs)).thenReturn(configuration);
}
Views
Replies
Total Likes
@Anderson_Hamer @SureshDhulipudi @arunpatidar
I am facing similar issue here, I am getting the value in 'valueMap' but getInherited() method is not working in Junit class. Please suggest.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies