Posts

Showing posts with the label Sling model

@Inject : A discouraged sling-model annotation

Image
"I just copy and paste it from somewhere and since it was working so I keep it like this. 😎"           @ Model (adaptables = Resource . class )      public class ExampleModel { @ Inject private MyService myService ; @ Inject private String title ; // ... rest of the class } I got this reply from the developer while reviewing his code above so thought of writing one extension post of my previous blog  Common Myths Around Sling Model .  So lets dive into the working mechanism of @Inject annotation but before that I would like to explain the injector first.                                                                      An OOB injector      @ Comp...

Common cause for NullPionterException or Null value in Junit test case for sling model in aem

Image
  Below are the common cause for NullPointerException and Null value in Junit test for Sling model: Problem with the adaptables Let say your sling model is adaptables to sling Resource and in your test class you are trying to get the instance of your respective Object by adapting request of aemContext. In this case you will end up with NullPointerException.  For example: Sling Model : @Model ( adaptables = { Resource .class}, adapters = { SolutionHeroComponent .class}, resourceType = { SolutionHeroComponentImpl .RESOURCE_TYPE}, defaultInjectionStrategy = DefaultInjectionStrategy .OPTIONAL ) @Exporter ( name = "jackson" , selector = "solutionhero" , extensions = "json" ) public class SolutionHeroComponentImpl implements SolutionHeroComponent { protected static final String RESOURCE_TYPE = "myproject/components/solutionhero" ; @Inject @Named (SOLUTION_HERO_TITLE) ...