Junit Test for sling delegation pattern is not working | Community
Skip to main content
Level 2
February 16, 2024
Solved

Junit Test for sling delegation pattern is not working

  • February 16, 2024
  • 2 replies
  • 690 views

Hi, 

 

I am trying to write test for custom image component. Here is the model impl

 

@Model( adaptables = SlingHttpServletRequest.class, adapters = {Image.class, ComponentExporter.class}, resourceType = ImageImpl.RESOURCE_TYPE, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL ) @Exporter( name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION ) public class ImageImpl implements Image { public static final String RESOURCE_TYPE = "myproject/components/content/image/v1/image"; @Self @2434638(type = ResourceSuperType.class) @delegate-1(excludes = ComponentDelegateExclusion.class) private com.adobe.cq.wcm.core.components.models.Image image; @ValueMapValue @1497330(booleanValues = false) private boolean alignCenter; @ValueMapValue @1497330(booleanValues = false) private boolean removeGap; @ValueMapValue @1497330(booleanValues = false) private boolean fluidImage; @9944223 public boolean getAlignCenter() { return alignCenter; } @9944223 public boolean getRemoveGap() { return removeGap; } @9944223 public boolean getFluidImage() { return fluidImage; } @9944223 public String getExportedType() { return RESOURCE_TYPE;

 

And this is my test class

 

@ExtendWith({AemContextExtension.class, MockitoExtension.class}) class ImageImplTest { private final AemContext context = new AemContextBuilder().plugin(CORE_COMPONENTS) .build(); private static final String TEST_BASE = "/components/image"; private ValueMap jsonProperties; private Image image; @BeforeEach void setUp() { context.addModelsForClasses(com.adobe.cq.wcm.core.components.models.Image.class); context.addModelsForClasses(ImageImpl.class); context.load().json(TEST_BASE + "/image-comp.json", "/apps/" + ImageImpl.RESOURCE_TYPE); Resource imageResource = context.load().json(TEST_BASE + "/image.json", "/content/image"); jsonProperties = imageResource.getValueMap(); context.currentResource("/content/image"); image = context.request().adaptTo(Image.class); } @2785667 void getAlignCenter() { assertEquals(jsonProperties.get("alignCenter"), Boolean.toString(image.getAlignCenter())); } @2785667 void getRemoveGap() { assertEquals(jsonProperties.get("removeGap"), Boolean.toString(image.getRemoveGap())); } @2785667 void getFluidImage() { assertEquals(jsonProperties.get("fluidImage"), Boolean.toString(image.getFluidImage())); } @2785667 void testResourceType() { assertEquals(jsonProperties.get("sling:resourceType"), image.getExportedType()); } }

 

The tests are working fine for custom properties I have added, but the  private com.adobe.cq.wcm.core.components.models.Image image is always null. I can't access any core components property. image-comp.json is the infinity.json of /apps/myproject/components/content/image/v1/image and image.json is the infinity.json from /content/***/image

How can I solve this?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
February 16, 2024
TarunKumar
Community Advisor
Community Advisor
February 16, 2024

Hi @julkar__naim ,

Try to write getter public function for all the core component private function and then try to access it in your JUNIT class. 
Also make sure you have these property mentioned in JSON resource file.

Thanks
Tarun