Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Error while compiling a serivce

Avatar

Level 1

Hi all,

I am getting the error while creating the service in AEM, as I have created the interface and when I am calling it in my model class it's throwing the error.

package com.aem.demo.core.models;

import javax.annotation.PostConstruct;

import javax.inject.Inject;

import javax.inject.Named;

import com.aem.demo.core.KeyService;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.annotations.Default;

import org.apache.sling.models.annotations.Model;

import org.apache.sling.settings.SlingSettingsService;

@Model(adaptables=Resource.class)

public class HelloWorldModel {

@Inject

private KeyService keyService ;

    @Inject

    private SlingSettingsService settings;

    @Inject @Named("sling:resourceType") @Default(values="No resourceType")

    protected String resourceType;

    private String message;

    @PostConstruct

    protected void init() {

  

   keyService.setKey(80);

  

        message = "\tHello World!-value of key service is"+keyService.getKey()+"\n";

        message += "\tThis is instance: " + settings.getSlingId() + "\n";

        message += "\tResource type is: " + resourceType + "\n";

    }

    public String getMessage() {

        return message;

    }

}

After compiling the above code

Errors:

[ERROR]   TestHelloWorldModel.setup:49 » NullPointer

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi,

how does your testclass look like? Remember, that if you just call the constructor of your model class and then call methods on it, the references and injections are not resolved.

Please see [1], [2] and [3] for references how to create unittests with sling models and SlingContext/AemContext.

HTH,

Jörg

[1] Writing unit tests for AEM — using SlingMocks – Things on a content management system

[2] Writing unit tests for AEM (part 2): Maven Setup – Things on a content management system

[3] Writing unittests for AEM (part 3): Mocking resources – Things on a content management system

View solution in original post

2 Replies

Avatar

Correct answer by
Employee Advisor

Hi,

how does your testclass look like? Remember, that if you just call the constructor of your model class and then call methods on it, the references and injections are not resolved.

Please see [1], [2] and [3] for references how to create unittests with sling models and SlingContext/AemContext.

HTH,

Jörg

[1] Writing unit tests for AEM — using SlingMocks – Things on a content management system

[2] Writing unit tests for AEM (part 2): Maven Setup – Things on a content management system

[3] Writing unittests for AEM (part 3): Mocking resources – Things on a content management system

Avatar

Community Advisor

I have a sample Unit Test code to test the Sling Model with Services.

I will share it with you for reference.



Arun Patidar