How to setProperty() in node object in AEM Sling Model when doing Junit Test?

I have tried to set the property in the the node class in my unit test but was not able to do so. I would like to seek your help in sharing your knowledge on setting node property in AEM Sling MOdel for unit test if you happen to do so before. Thank you!

The implementation is as follows:

@Model(adaptables = SlingHttpServletRequest.class)
public class SlingModelClass {

    private SomeService someService;
    private boolean isShowCaptcha;

    private Resource resource;

    private Node node;

    public void setup() {
        node = resource.adaptTo(Node.class);
        isShowCaptcha = someService.isCaptchaHide();
        hideShowTitle(!isShowCaptcha);       <= Test unable to get through this method

    public boolean getIsShowCaptcha() {
        return this.isShowCaptcha;

    public PropertyIterator getPropertiesItems() throws RepositoryException {
        return this.node.getProperties();

    private void hideShowTitle(boolean flag) {
        try {
            node.setProperty("hideTitle", flag);     <= I am getting a null pointer exception at this line
            if(flag) {
                node.setProperty("guideNodeClass", "");
            } else {
                node.setProperty("guideNodeClass", "guideCaptcha");

        } catch (RepositoryException e) {

The unit test that I have written:

public class SlingModelClassTest {

    public final AemContext context = new AemContext();

    private SomeService someService = mock(SomeService .class);

    Resource resource;

    // injects all the mocks into the tested object.
    private SlingModelClass slingModelClass;

    private Node node;

    private static String PATH = "/content/testproject/en/page/sub-page";

    public void setUp() {
        context.load().json("/components/textrte.json", PATH);
        resource = mock(Resource.class);

    public void testSetup() {

        context.registerService(SomeService.class, someService);


The error is as follows:

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(
    at org.junit.internal.runners.statements.RunBefores.evaluate(
    at org.junit.rules.ExternalResource$1.evaluate(
    at org.junit.runners.ParentRunner$3.evaluate(
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(
    at org.junit.runners.ParentRunner.runLeaf(
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(
    at org.junit.runners.ParentRunner$
    at org.junit.runners.ParentRunner$1.schedule(
    at org.junit.runners.ParentRunner.runChildren(
    at org.junit.runners.ParentRunner.access$100(
    at org.junit.runners.ParentRunner$2.evaluate(
    at org.junit.runners.ParentRunner$3.evaluate(
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(
    at com.intellij.rt.execution.junit.JUnitStarter.main(

1 answer

  • answered 2019-12-02 06:40 Florian Salihovic

    This is the wrong pattern. Sling models should not mutate the JCR node. You might get into big trouble when every time a Sling model is instantiated it performs write operations on the repo. Sling model are just supposed to read from the JCR.

    If you need to have some global flag, use the page properties and inheritance to read the flag and overwrite it if necessary. This way, the configuration is done in a controlled way and you only write the property when necessary.

    Just to complete my answer, as suggested by @OliverGebert : the Sling API should be used to work with repository data/as the abstraction layer. The JCR API is considered to be too low level and AEM is basically built on top of Sling. Also, you would need to take care of managing Sling as well as JCR dependencies, which can be a big hassle in long term projects.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum