Want to write test cases for init() | Community
Skip to main content
keshava219
Level 3
December 21, 2023
Solved

Want to write test cases for init()

  • December 21, 2023
  • 2 replies
  • 849 views

@PostConstruct
protected void init() {


          String currentPageResource = currentPage.getPath()";
          Resource resourceContainer = resolver.getResource(currentPageResource);
          Iterator<Resource> currentPageContainer = resourceContainer.listChildren();
if (currentPageContainer != null) {
         currentPageContainer.forEachRemaining(item -> items.add(item.adaptTo(modelitem.class)));
           items = items.stream().filter(modelitem::isAddToLocal).collect(Collectors.toList());
          }
}

 

any references please

 

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 Harwinder-singh

@keshava219  Your  junit should look something like this; 

 

 

@Mock
private Page currentPage;

@Mock
private ResourceResolver resolver;

@InjectMocks
private InitTest initTest;

private List<modelitem> items;

@BeforeEach
public void setup() {
MockitoAnnotations.initMocks(this);
items = new ArrayList<>();
}

@Test
public void testInitMethod() {

String pagePath = "/content/abc/def";
when(currentPage.getPath()).thenReturn(pagePath);

Resource resourceContainer = mock(Resource.class);
when(resolver.getResource(pagePath)).thenReturn(resourceContainer);

List<Resource> childrenResources = new ArrayList<>();
Resource childResource1 = mock(Resource.class);
Resource childResource2 = mock(Resource.class);
childrenResources.add(childResource1);
childrenResources.add(childResource2);

Iterator<Resource> iterator = childrenResources.iterator();
when(resourceContainer.listChildren()).thenReturn(iterator);

modelitem modelItem1 = mock(modelitem.class);
modelitem modelItem2 = mock(modelitem.class);
when(modelItem1.isAddToLocal()).thenReturn(true);
when(modelItem2.isAddToLocal()).thenReturn(false);

// Mocking adaptTo method for modelitem class
when(childResource1.adaptTo(modelitem.class)).thenReturn(modelItem1);
when(childResource2.adaptTo(modelitem.class)).thenReturn(modelItem2);

// Call the method to be tested
initTest.init();


List<modelitem> filteredItems = items.stream().filter(modelitem::isAddToLocal).collect(Collectors.toList());
assertEquals(1, filteredItems.size()); // Assuming only one model item returns true for isAddToLocal() . This can be modified based on the expected outcome.

}

2 replies

Harwinder-singh
Community Advisor
Harwinder-singhCommunity AdvisorAccepted solution
Community Advisor
December 23, 2023

@keshava219  Your  junit should look something like this; 

 

 

@Mock
private Page currentPage;

@Mock
private ResourceResolver resolver;

@InjectMocks
private InitTest initTest;

private List<modelitem> items;

@BeforeEach
public void setup() {
MockitoAnnotations.initMocks(this);
items = new ArrayList<>();
}

@Test
public void testInitMethod() {

String pagePath = "/content/abc/def";
when(currentPage.getPath()).thenReturn(pagePath);

Resource resourceContainer = mock(Resource.class);
when(resolver.getResource(pagePath)).thenReturn(resourceContainer);

List<Resource> childrenResources = new ArrayList<>();
Resource childResource1 = mock(Resource.class);
Resource childResource2 = mock(Resource.class);
childrenResources.add(childResource1);
childrenResources.add(childResource2);

Iterator<Resource> iterator = childrenResources.iterator();
when(resourceContainer.listChildren()).thenReturn(iterator);

modelitem modelItem1 = mock(modelitem.class);
modelitem modelItem2 = mock(modelitem.class);
when(modelItem1.isAddToLocal()).thenReturn(true);
when(modelItem2.isAddToLocal()).thenReturn(false);

// Mocking adaptTo method for modelitem class
when(childResource1.adaptTo(modelitem.class)).thenReturn(modelItem1);
when(childResource2.adaptTo(modelitem.class)).thenReturn(modelItem2);

// Call the method to be tested
initTest.init();


List<modelitem> filteredItems = items.stream().filter(modelitem::isAddToLocal).collect(Collectors.toList());
assertEquals(1, filteredItems.size()); // Assuming only one model item returns true for isAddToLocal() . This can be modified based on the expected outcome.

}