Error while compiling a serivce

Avatar

Avatar

kunald48906615

Avatar

kunald48906615

kunald48906615

18-11-2019

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

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

916

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

916

Correct Answer

1.0K
Jörg_Hoh
Employee

19-11-2019

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

Answers (1)

Answers (1)

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

21-11-2019

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

I will share it with you for reference.