How to write junit for Collection<ReferenceSearch.Info> | Community
Skip to main content
Level 4
May 21, 2024
Solved

How to write junit for Collection<ReferenceSearch.Info>

  • May 21, 2024
  • 2 replies
  • 1487 views

Hi @16227148

 

At line collection<ReferenceSearch.Info> resultSet ... getting NPE in test class.

replicationType = Deactivate;

public void handleReferences(String path, ResourceResolver resourceResolver, String eventType) {
ReferenceSearch referenceSearch = new ReferenceSearch();
referenceSearch.setExact(true);
referenceSearch.setHollow(true);
referenceSearch.setMaxReferencesPerPage(-1);
Collection<ReferenceSearch.Info> resultSet = referenceSearch.search(resourceResolver, path).values();
String[] references = resultSet.stream().map(ReferenceSearch.Info::getPagePath).distinct().toArray(String[]::new);

Arrays.stream(references)
.filter(reference -> StringUtils.startsWith(reference, "/content/dam/content-fragment"))
.forEach(reference -> checkMarketValue(path, resourceResolver, reference, eventType));
}

How to write junit for this type of methods.?

 

Thanks

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lone_Ranger

@lone_ranger , I didn't see any mock construction in your test class, please use mock construction for 

ReferenceSearch

as suggested in earlier comments to cover that method. Simple mock won't work here.


Moking replicationActionMockedStatic worked for me as it was giving NPE here.

2 replies

sravs
Community Advisor
Community Advisor
May 21, 2024

@lone_ranger , try using mockedConstruction to create a constructor, please find sample code

try (MockedConstruction refSearch = mockConstruction(ReferenceSearch.class)) { ReferenceSearch refSearch = new ReferenceSearch(); when(refSearch.search(any(ResourceResolver.class), anyString())) .thenReturn(//mock object); // Your method call }

Refer for more details about mockedConstruction

https://www.baeldung.com/java-mockito-constructors-unit-testing

Level 4
May 21, 2024

Hi @sravs  thanks for reply but this is not working 

 

try (MockedConstruction refSearch = mockConstruction(ReferenceSearch.class)) {
ReferenceSearch refSearchx = new ReferenceSearch();
when(refSearchx.search(any(ResourceResolver.class), anyString()))
.thenReturn(refSearch);

i tried this code.

 

sravs
Community Advisor
Community Advisor
May 22, 2024

The error is at below line

 when(refSearchx.search(any(ResourceResolver.class), anyString()))
.thenReturn(refSearch)

You should return mock object of the return type of search method not the refSearch object, Create a mock object for 

java.util.Map<java.lang.String,ReferenceSearch.Info>

like 

 

@Mock java.util.Map<java.lang.String,ReferenceSearch.Info> refSearchMap

 

 and update the statement 

when(refSearchx.search(any(ResourceResolver.class), anyString()))
.thenReturn(refSearchMap);
//write the when and thenReturn statements for the methods called on refSearchMap
kautuk_sahni
Community Manager
Community Manager
June 7, 2024

@lone_ranger I hope you found the AEM community helpful. We look forward to seeing you return as either a learner or a mentor. The community flourishes with SMEs like you. Happy AEM learning!

Kautuk Sahni