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
BedrockMission!

Learn More

View all

Sign in to view all badges

wcm.io Junit4 Test - Sling Model Constructor

Avatar

Avatar
Validate 1
Level 1
karthik4
Level 1

Likes

2 likes

Total Posts

30 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile

Avatar
Validate 1
Level 1
karthik4
Level 1

Likes

2 likes

Total Posts

30 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile
karthik4
Level 1

12-05-2020

Hello, I am having a problem where the resource is not injected into the Sling Model constructor during a test, I was able to inject mock services into the constructor, but unable to with the resource object (I do not wish to inject the Resource via Sling Model Annotations, as I want to use the  constructor only):

WebData Sling Model:

@Model(adaptables = {SlingHttpServletRequest.class, Resource.class},
        defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class WebData {
    @inject
    public WebData(Resource resource) {
        
    }
}

 SlingModel Unit Test:

@test
public void itShouldInject() {
     Resource resource = context.resourceResolver().getResource("/content/my-site/page/jcr:content/par/web");
     undertest =  resource.adaptTo(WebData.class);
}

 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

611 likes

Total Posts

588 posts

Correct Reply

230 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

611 likes

Total Posts

588 posts

Correct Reply

230 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

12-05-2020

Hey,

I know you said only constructor solutions, but I had a problem like this in the past, where It was extremely challenging to mock the Resource.class object into the constructor. Instead of initiating the Resource object dependency from the constructor, try using these Sling Model Annotations.

 

 

@Model
public class WebData {
   // this adapts the adaptable object into a resource object.
   @Self
   private Resource resource;
   
   //or

   @ScriptVariable
   // this calls on the current resource.
   Resource resource;
}

 

 

for other Sling Model annotations, checkout this blog https://sourcedcode.com/aem-sling-model-injectors-annotations-reference-guide

 

Answers (1)

Answers (1)

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,348 likes

Total Posts

3,226 posts

Correct Reply

916 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,348 likes

Total Posts

3,226 posts

Correct Reply

916 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

12-05-2020

Hi,

Can you try with -

 

private final AemContext context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);