@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
Solved! Go to Solution.
Views
Replies
Total Likes
@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.
}
Hi @keshava219
Please check following example
@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.
}
Views
Likes
Replies