Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Testing
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Testing
Mock Nested Method
vivek dhiman
Ranch Hand
Posts: 157
I like...
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi, I was writting
test
case for my controller class. I have method is my controller class as :
public BaseModel getModel( ) { listChildren = getModelItems(); return ModelBuilder.build ( rcm -> rcm.setCtaLinkList(listChildren) ); } public List<myType> getModelItems(){ myType myTypeObj; Page childPage; String description; Page currentPage = getCurrentPage(); List<myType>listChildPages = new ArrayList<>(); Iterator<Page> childPages = currentPage.listChildren(); while (childPages.hasNext()) { myTypeObj = new myType(); childPage = childPages.next(); myTypeObj.setLinkText(childPage.getTitle()); myTypeObj.setLink(childPage.getPath()); java.util.Optional<String> stringToUse = java.util.Optional.ofNullable(childPage.getContentResource().getValueMap().get("jcr:description") ).map(Object::toString); if (stringToUse.isPresent()){ description = stringToUse.get(); myTypeObj.setDescription(description); } listChildPages.add(myTypeObj); } return listChildPages; } }
I am creating the test case but giving NPE got getCurrentPage. Below is the test code.
@InjectMocks ActionController actionController ; @Test public void ReturnModel() { when(actionController.getCurrentPage()).thenReturn(getMockedCurrentPage()); ActionModel actionModel = (ActionModel)actionController.getModel(); assertEquals(actionModel instanceof ActionModel, is(true)); } private Page getMockedCurrentPage() { Page currentPage = mock(Page.class); when(resource.getResourceResolver()).thenReturn(resourceResolver); PageManager pageManager = mock(PageManager.class); when(resourceResolver.adaptTo(PageManager.class)).thenReturn(pageManager); when(currentPage.getPath()).thenReturn("/currentPage/path"); when(pageManager.getContainingPage(resource)).thenReturn(currentPage); return currentPage; } private List<MyModel> getMockedModelItems(){ MyModel myModel1 = mock(MyModel.class); MyModel myModel2 = mock(MyModel.class); Page childPage = mock(Page.class); Page currentPage = getMockedCurrentPage(); List<MyModel>listChildPages = new ArrayList<>(); when(myModel1.getLink()).thenReturn("/content"); when(myModel1.getLinkText()).thenReturn("This is link1"); when(myModel1.getDescription()).thenReturn("This is Description"); when(myModel2.getLink()).thenReturn("/content"); when(myModel2.getLinkText()).thenReturn("This is link2"); when(myModel2.getDescription()).thenReturn("This is Description"); listChildPages.add(myModel1); listChildPages.add(myModel2); return listChildPages; }
Any Idea how can i achieve mocking of getModel() method.
Thanks
"To do good, you actually have to do something." -- Yvon Chouinard
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
TestCase Fail : Actual Result Null
TestCase Error Mokito
How to Test Reader read() function
Test ResponseBody return List
Mockito Help - Problem to sort a List of mock objects.
More...