hi experts,
May i know how to mock below code in Sling Model using AEMContext Mock, Thanks
@SlingObject
ResourceResolver resourceResolver.
Resource resource = resourceResolver.getResource("/content/dam/cf");
Solved! Go to Solution.
Hi @shyams67489715 ,
Try this,
@ExtendWith(AemContextExtension.class) public class ExampleTest { private final AemContext context = new AemContext();
@BeforeEach
void setUp() throws Exception {
context.load().json("/path/to/your/json/resource.json", "/content/dam/cf");
}
@Test
public void testSomething() {
Resource resource = context.resourceResolver().getResource("/content/dam/cf");
// further testing
}
}
Hope that helps!
Regards,
Santosh
Hi @shyams67489715 ,
Try this,
@ExtendWith(AemContextExtension.class) public class ExampleTest { private final AemContext context = new AemContext();
@BeforeEach
void setUp() throws Exception {
context.load().json("/path/to/your/json/resource.json", "/content/dam/cf");
}
@Test
public void testSomething() {
Resource resource = context.resourceResolver().getResource("/content/dam/cf");
// further testing
}
}
Hope that helps!
Regards,
Santosh
Hi,
You can add the json and map to the resource
public final AemContext ctx = new AemContext();
ctx.load().json("/com/myproject/models/damresource/damFile.json", "/content/dam/cf");
Resource mockDamResource = ctx.resourceResolver().getResource("/content/dam/cf");
The AemContext object provides access to mock implementations of:
Additionally it supports:
Example
public class ExampleTest { @Rule public final AemContext context = new AemContext(); @Test public void testSomething() { Resource resource = context.resourceResolver().getResource("/content/sample/en"); Page page = resource.adaptTo(Page.class); // further testing } }
The AEM mock context supports different resource resolver types (provided by the Sling Mocks implementation). Example:
private final AemContext context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
Reference: https://wcm.io/testing/aem-mock/usage.html
Thanks
Views
Likes
Replies
Views
Likes
Replies