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
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

JUnit for resourceResolver.getResource(filePath)

Avatar

Level 5

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
Level 9

 

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
Level 9

 

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 5

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.