Expand my Community achievements bar.

SOLVED

Asset asset = assetManager.getAsset("path"); giving null in Junit test cases

Avatar

Level 2

Junit class 

@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
context.load().json("/pdf.json", "/content/dam");
context.currentResource("/content/dam");
AssetManager assetManager =context.resourceResolver().adaptTo(AssetManager.class);
Asset asset=context.resourceResolver().adaptTo(Asset.class);
//when(assetManager.getAsset("/content/dam/aemgeeks/file-sample_150kB.pdf")).thenReturn(assetManager.getAsset("/content/dam/aemgeeks/file-sample_150kB.pdf"));
request = new MockSlingHttpServletRequest(context.resourceResolver());

}

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @prakashvb ,

 

Try below bold libes of code-

 

@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
context.load().json("/pdf.json", "/content/dam");
context.currentResource("/content/dam");
AssetManager assetManager =context.resourceResolver().adaptTo(AssetManager.class);
//Asset asset=context.resourceResolver().adaptTo(Asset.class);

Asset asset = context.create().asset("/content/dam/aemgeeks/file-sample_150kB.pdf", 1, 1, "application/pdf");
when(assetManager.getAsset("/content/dam/aemgeeks/file-sample_150kB.pdf")).thenReturn(asset);


request = new MockSlingHttpServletRequest(context.resourceResolver());

}

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @prakashvb ,

 

Try below bold libes of code-

 

@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
context.load().json("/pdf.json", "/content/dam");
context.currentResource("/content/dam");
AssetManager assetManager =context.resourceResolver().adaptTo(AssetManager.class);
//Asset asset=context.resourceResolver().adaptTo(Asset.class);

Asset asset = context.create().asset("/content/dam/aemgeeks/file-sample_150kB.pdf", 1, 1, "application/pdf");
when(assetManager.getAsset("/content/dam/aemgeeks/file-sample_150kB.pdf")).thenReturn(asset);


request = new MockSlingHttpServletRequest(context.resourceResolver());

}