Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

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

Avatar

Administrator

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.

0 Replies