Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

JUnit for resourceResolver.getResource(filePath)

Avatar

Level 6

My JUnit test is getting stuck in this section resourceResolver.getResource(filePath). My Mock AEM Context resource is having page path, its component details and in this section  resourceResolver.getResource(filePath), I am trying to fetch an asset. How can I handle this?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

 

Sample code

 

@ExtendWith({ AemContextExtension.class, MockitoExtension.class })
public class SampleTest {

	private final AemContext aemContext = new AemContext(ResourceResolverType.JCR_MOCK);

	@Mock
    private Resource resource;
	
	@Mock
	private SlingHttpServletRequest request;

	@Mock
	private ResourceResolver resourceresolver;
	
	@BeforeEach
	void setUp() throws Exception {
		lenient().when(request.getResourceResolver()).thenReturn(resourceresolver);
		aemContext.addModelsForClasses(Sample.class);
		aemContext.load().json("/sample/component.json", "/content");
		aemContext.load().json("/sample/assets.json", SAMPLE_DAM_PATH);	
Mockito.when(resourceResolver.getResource(ASSET_PATH_FROM_JSON)).thenReturn(resource); } }

 

 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

 

Sample code

 

@ExtendWith({ AemContextExtension.class, MockitoExtension.class })
public class SampleTest {

	private final AemContext aemContext = new AemContext(ResourceResolverType.JCR_MOCK);

	@Mock
    private Resource resource;
	
	@Mock
	private SlingHttpServletRequest request;

	@Mock
	private ResourceResolver resourceresolver;
	
	@BeforeEach
	void setUp() throws Exception {
		lenient().when(request.getResourceResolver()).thenReturn(resourceresolver);
		aemContext.addModelsForClasses(Sample.class);
		aemContext.load().json("/sample/component.json", "/content");
		aemContext.load().json("/sample/assets.json", SAMPLE_DAM_PATH);	
Mockito.when(resourceResolver.getResource(ASSET_PATH_FROM_JSON)).thenReturn(resource); } }

 

 

Avatar

Level 6

what will be the content structure for  /sample/assets.json , considering assets (nt:file) will have rendition/original etc.

Because I need to adapt the asset original to input stream.