• 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

Test ResponseBody return List

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How could I test this method in my controller:


I tried with this code, but I get a NullPointerException with the employRepository List.



employRepository return a list of 2 elemets, i don't know why when calling the getEmployeeList() from Test method with mockmvc , Emp_All is not populated?
Any help would be much appreciated
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where in that tells the mock to return 2 employees?
Or anything in fact.
'null' is the default return value from a mock call that hasn't been set up yet.
 
Alex Giovi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm writing this because just toexplain that when i test just the employRepository.findAll() statement in the controller class
it returns 2 employee but when i tried to test this controller from another class test , i still have NPE
on this same statement,sincerely i don't know if i have to include a test repository statement in my test method ,
and how could be with mockmvc,i thought the perform.get will call the main controller or method, execute
the empRepository statement and return a list but the origin of NPE in this case is anonymous for me,
is there another way to test this method?
Thanks
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know the Spring MVC mocking framework, but any mocking framework requires you to provide (if needed) a return value when a method on a mocked object is called.

You say you have an NPE occurring. I assume this is the List (Emp_All), if not then you'll have to provide some more info as to what is null.
If I'm correct, then Emp_All is null because it looks to me as if you haven't provided any return value via the mocked employRepository.

Looking at this SO thread it looks like you need to have a mocked version of your employRepository accessible.
Then you can set up some expectations (when(employRepo.findAll()).thenReturn(your list of employees)).
 
Alex Giovi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave,i added a mock to repository in test code but it doesn't work,
the problem that i still have the same error , and in debug mode,the compiler doesn't reach the getEmployeeList() method with url provided in mockMvc
i'll try seraching for solution
 
Always look on the bright side of life. At least this ad is really tiny:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic