Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Junit for sling models based on Delegation Pattern

Avatar

Level 4
Hi Team,
 
We are trying to create junit for sling models where delegate pattern was used, but the core class reference always returns null.Is there any better way to create junit for such use case. Detailed error : java.lang.NullPointerException: Cannot invoke "com.adobe.cq.wcm.core.components.models.Navigation.getAccessibilityLabel()" because "this.navigation" is null.
 
 
modeltest.png
1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi @NehaCMS ,

 

You can check the WKND project example for ImageList, It injects "com.adobe.cq.wcm.core.components.models.List" 

https://github.com/adobe/aem-guides-wknd/blob/main/core/src/test/java/com/adobe/aem/guides/wknd/core... 

 

Hope this helps!

 

Regards,

Nitesh

View solution in original post

8 Replies

Avatar

Correct answer by
Employee Advisor

Hi @NehaCMS ,

 

You can check the WKND project example for ImageList, It injects "com.adobe.cq.wcm.core.components.models.List" 

https://github.com/adobe/aem-guides-wknd/blob/main/core/src/test/java/com/adobe/aem/guides/wknd/core... 

 

Hope this helps!

 

Regards,

Nitesh

Avatar

Level 4

hi @nitesh_kumar - Tried but getting null pointer for imagelist core as well, can you check or have any working sample to be shared.

NehaCMS_0-1675227567715.png

 

Avatar

Level 4

hi @arunpatidar - Have tried above, bit it gives 

01:27:12.041 [main] DEBUG org.apache.sling.testing.mock.sling.MockAdapterManagerImpl - Using adapter factory org.apache.sling.models.impl.ModelAdapterFactory@4232b34a to map org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest@2da16263 to class test.models.components.CustomTeaser

java.lang.NullPointerException

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaser.getTitle(CustomTeaser.java:55)

at test.models.components.CustomTeaserTest.testCustomTeaser(CustomTeaserTest.java:40)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.base/java.lang.reflect.Method.invoke(Method.java:566)

at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)

at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)

at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)

at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)

at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)

at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)

at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)

at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)

at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)

at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)

at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)

at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)

at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)

at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)

at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:212)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208)

at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)

at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)

at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)

at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)

at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)

at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)

at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)

at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)

at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)

at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)

at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)

at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)

at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)

at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:141)

at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

at

FieldSetter.setField(customTeaser2, customTeaser2.getClass().getDeclaredField("teaser"), teaser.getTitle());

so giving null for core component teaser class fields, can you help in checking