Posts

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) ...

"java.lang.NoSuchFieldError: FACTORY" while running Junit test in AEM codebase

 I was implementing Unit testing for a already running AEM project. After adding all the dependencies and after creation of basic structure for Sling model test class my test gets executed as expected. @ ExtendWith ({ AemContextExtension . class , MockitoExtension . class }) class DummyComponentImplTest { @ BeforeEach void setUp () { } @ Test public void testGetHeading () { } } But soon as I add below line at line #25 in the above running test case private final AemContext aemContext = new AemContext (); and try to run the test in terminal getting below error in the terminal's maven console. Running com . myproject . aem . models . impl . DummyComponentImplTest [ERROR] Tests run : 1 , Failures : 0 , Errors : 1 , Skipped : 0 , Time elapsed : 2.258 s <<< FAILURE! - in com . myproject . aem . models . impl . DummyComponentImplTest [ERROR] testGetHeading Time elapsed : 0.007 s <<< ERROR! java . lang . NoSuchFieldError...

Common Myths Around Sling Model

Image
Fortunately I got a chance to talk to several people from AEM fraternity to understand their take about sling model and below are the summery of those discussions: Sling model is only back-end business logic provider to AEM components. Adaptable are just a way to get respective sling object reference. For example if you need slingRequest object in sling model then make your sling model adaptable to HttpSlingServletRequest. You can not get a value from sling model in a servlet or in any other class. When you need a reference of an OSGI service, you have only one way, that is @ OSGiService annotation. You can export only JSON data thru sling model exporter. You can not extend one sling model into another sling model. Sling model can to be created outside of the designated package in AEM. One Interface can not be a Sling Model, whenever you need to create a Sling Model you will have to write a pure POJO concrete implemented java class. You can only read properties of a node thru sling...