JUnit 4: AEM How to Mock @RequestAttribute value in Sling Model

This blog article will show code an example for JUnit 4, for how the Request Attribute is being mocked. This example will only cover mocking the @RequestAttribute with the use of @PostConstruct runtime phase, When you are in the code runtime phase of the constructor injection, this will not work (I spent a bit of time on this).

Attention
This article will not cover how to setup JUnit 4 for AEM, but however, more information can be found on this blog article.

Sling Model Class : ExampleSlingModel.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.sourcedcode.core.models;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.RequestAttribute;

import javax.annotation.PostConstruct;

@Model(adaptables = SlingHttpServletRequest.class,
        defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ExampleSlingModel {

    @RequestAttribute(name = "websitename")
    private String websiteName;

    @RequestAttribute
    private int version;

    @PostConstruct
    public void init() {
        websiteName = websiteName.concat(":websitename");
        version = version * 2;
    }

  public String getWebsiteName() {
    return websiteName;
  }

    public int getVersion() {
        return version;
    }
}

Sling Model Test Class : ExampleSlingModelTest.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.sourcedcode.core.models;

import io.wcm.testing.mock.aem.junit.AemContext;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

public class ExampleSlingModelTest {

    @Rule
    public final AemContext aemContext = new AemContext(ResourceResolverType.JCR_MOCK);

    private MockSlingHttpServletRequest request;

    private ExampleSlingModel underTest;

    @Before
    public void setUp() throws Exception {
        aemContext.addModelsForPackage("com.finning.platform.core.models.utils");
        request = aemContext.request();
        request.setAttribute("websitename", "sourcedcode");
        request.setAttribute("version", 5);
    }

    @Test
    public void test_getWebsiteName() {
        underTest = request.adaptTo(ExampleSlingModel.class);
        Assert.assertEquals("sourcedcode:websitename", underTest.getWebsiteName());
    }

    @Test
    public void test_getVersion() {
        underTest = request.adaptTo(ExampleSlingModel.class);
        Assert.assertEquals(10, underTest.getVersion());
    }
}

Looking for Help? Or need a mentor?

If you need help to super drive your AEM career road map, you can hire me for a one-hour session on codementor.io; We can work together on a price works just for you. https://www.codementor.io/@briankasingli. I'd be happy to help you along your way to becoming a high-quality AEM full-stack engineer.

Was this post helpful?

Hello, I am an enthusiastic Adobe Community Advisor and a seasoned Lead AEM Developer. I am currently serving as an AEM Technical Lead at MNPDigital.ca, bringing over a decade of extensive web engineering experience and more than eight years of practical AEM experience to the table. My goal is to give back to the AEM Full Stack Development community by sharing my wealth of knowledge with others. You can connect with me on LinkedIn.

17 thoughts on “JUnit 4: AEM How to Mock @RequestAttribute value in Sling Model

  1. It’s a shame you don’t have a donate button! I’d most certainly donate to this outstanding blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to brand new updates and will share this site with my Facebook group. Chat soon!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top