Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Spring Controller @ResponseBody is Empty

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my spring webapp, I'm trying to test my controller:

My test Code looks like this:



When i execute the test i got this:


when i test the url in Chrome Browser i got this:

[{"id":435651,"Position_id":33,"section":64509}]

In my pom.xml file i added jackson-mapper-asl.jar and jackson-core-asl.jar I don't know why the response status is 200 but i did'nt got any data,so how the unit test pass?

Any help will be much appreciated
 
Rancher
Posts: 4625
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something doesn't look right there.
You are mocking the controller you want to test.
I'm going to guess that the result of that is Sping's call to the read method will result in a default response by the mocked controller.
I would have expected you to @InjectMocks into the controller, and @Mock the PositionRepository attribute?
 
Alex Giovi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,
thanks for the post , i'm a newbie in spring and unit testing, i thought @Mock will create a Reference instance to the original class,so i don't understand what's @InjeckMoks is used for?
so if you can explain a little bit what's the difference between them.
And it works:)

Now ,i updated my controller code, with a new line (to make it easy for me)

And




I know it's not a clear code , but i would like to have your opinion , if possible.
to execute LINE M in Mockito test method(Got a NPE in this line), do i have to Mock the others classes,Like what I do for the Repository interface
or is there any other method to include the execution of this line on code in Test ambiant
Also, i put a breakpoint in Read() method , and i started the appliaction in debug mode but it doesn't stop,is it
normal when using Mocking url?
Many thanks
 
Dave Tolls
Rancher
Posts: 4625
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Mock tells mockito you want to create a mock version of that class. That is, a version you can then handle all interaction with.
You don't mock out the class you are testing, you mock out the dependencies of that class.

Now, for Util it is going to be harder as it is a class of static methods, and mocking works best with interfaces.
It can be done, but in this case it's not necessary as all the class does is get something from the HttpSession...which you can mock out. So just mock that call and return a suitable InfoSession object (possibly itself another mocked object).

Have you read the Mockito docs?
 
Alex Giovi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for these informations.
I'll have a look at the documentation
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic