Expand my Community achievements bar.

SOLVED

How to write junit for Collection<ReferenceSearch.Info>

Avatar

Level 4

Hi @everyone

 

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

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 4
Moking replicationActionMockedStatic worked for me as it was giving NPE here.

View solution in original post

9 Replies

Avatar

Community Advisor

@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

Avatar

Level 4

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.

 

Avatar

Community Advisor

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

Avatar

Level 4

still it is not working.... test class is getting executed but no increment in line coverage

Avatar

Level 4
package ....

import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.wcm.commons.ReferenceSearch;
import com.ey.aem.unified.core.resource.ResourceResolverProvider;
import org.apache.sling.api.resource.ResourceResolver;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.OngoingStubbing;
import org.osgi.service.event.Event;

import java.util.Collection;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class PeopleProfileDeactivateEventHandlerTest {
@Mock
java.util.Map<java.lang.String, ReferenceSearch.Info> resultSet;
@Mock
Collection<ReferenceSearch.Info> resultSetValues;
@Mock
private ResourceResolverProvider resourceResolverProvider;
@InjectMocks
private PeopleProfileDeactivateEventHandler handler = new PeopleProfileDeactivateEventHandler();
@Mock
private ReferenceSearch referenceSearch;
// @Mock
// private ResourceResolver resourceResolver;

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

@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
ResourceResolver resourceResolver = Mockito.mock(ResourceResolver.class);
lenient().doReturn(resourceResolver).when(resourceResolverProvider).provideCoreServiceResolver();
}

@Test
public void testHandleEvent_DeactivateAction() {
Event event = mock(Event.class);
ResourceResolver resourceResolver = Mockito.mock(ResourceResolver.class);
lenient().doReturn(resourceResolver).when(resourceResolverProvider).provideCoreServiceResolver();
MockedStatic<ReplicationAction> replicationActionMockedStatic = Mockito.mockStatic(ReplicationAction.class);
ReplicationAction replicationAction = mock(ReplicationAction.class);
replicationActionMockedStatic.when(() -> ReplicationAction.fromEvent(any())).thenReturn(replicationAction);
lenient().when(replicationAction.getType()).thenReturn(ReplicationActionType.DEACTIVATE);
lenient().when(replicationAction.getPaths()).thenReturn(new String[]{"/content/dam/content-fragments/people/test"});
lenient().when(referenceSearch.search(any(), anyString())).thenReturn(resultSet);
handler.handleEvent(event);
assertNotNull(handler);
}

}

Avatar

Community Advisor

@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.

Avatar

Correct answer by
Level 4
Moking replicationActionMockedStatic worked for me as it was giving NPE here.

Avatar

Administrator

@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