JUnit 4: AEM How to Mock @RequestAttribute value in Sling Model | AEM Community Blog Seeding | Community
Skip to main content
kautuk_sahni
Community Manager
Community Manager
January 20, 2022

JUnit 4: AEM How to Mock @RequestAttribute value in Sling Model | AEM Community Blog Seeding

  • January 20, 2022
  • 0 replies
  • 952 views

BlogImage.jpg

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

Abstract

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

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;
}
}

Read Full Blog

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

Q&A

Please use this thread to ask the related questions.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.