Hello,
We have a requirement where we need to set some attributes to the session object through out the user journey and finally, we need to read the session attribute names and will read each attribute name.
Can someone suggest how I can mock these values:
Here is the sample code:
Fields fields= new Fields();
if (session != null) {
Enumeration<String> attributeNames = session.getAttributeNames();
List<FieldItems> positiveList = new ArrayList<>();
List<FieldItems> negativeList = new ArrayList<>();
while (attributeNames.hasMoreElements()) {
String attributeName = attributeNames.nextElement();
if (attributeName.startsWith(PAGE_PATH)) {
String attributeValue = session.getAttribute(attributeName).toString();
Solved! Go to Solution.
Views
Replies
Total Likes
Initialize AemContext object
private final AemContext context = new AemContext();
Get SlingHttpServletRequest object from context
SlingHttpServletRequest request = context.request();
request.getSession().setAttribute("key", "value");
For additional information refer official documentation here: Usage | wcm.io
Views
Replies
Total Likes
Hi @sateeshreddy,
You can use AEM Mocks from wcm.io - https://wcm.io/testing/aem-mock/
To create a mock session object and add attributes to it, see
https://wcm.io/testing/aem-mock/junit5/apidocs/index.html?io/wcm/testing/mock/aem/junit5/AemContext....
https://www.tabnine.com/code/java/classes/io.wcm.testing.mock.jcr.MockSession
I hope this helps.
Krass
Views
Replies
Total Likes
Initialize AemContext object
private final AemContext context = new AemContext();
Get SlingHttpServletRequest object from context
SlingHttpServletRequest request = context.request();
request.getSession().setAttribute("key", "value");
For additional information refer official documentation here: Usage | wcm.io
Views
Replies
Total Likes
@shubhanshu_singh It works. Thanks for swift response.
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies