
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
Q&A
Please use this thread to ask the related questions.
Kautuk Sahni