• 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Should I unit test these method if so, how?  RSS feed

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've built ToDoApp. I want to test my controller. I've got methods like:


I've tried to test them with mocking storage. I've made something like this:




For createUser I've got error like this:


and for getTasks:


Should I test these methods? If so, how should I improve my actual tests.
 
author & internet detective
Posts: 38918
686
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the User object have an equals method defined? If not, Mockito wouldn't see the objects as equal and would behave as you described.
 
must Janik
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean? I do not understand. Ye, objects are not equaled as we see on stacktrace, but how can I deal with it.
 
Jeanne Boyarsky
author & internet detective
Posts: 38918
686
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm saying you should write equals and hash code methods for this object.
 
must Janik
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I generated these method. What should I do know?
 
Jeanne Boyarsky
author & internet detective
Posts: 38918
686
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider adding a toString method to print the username (not the password) to get clearer error messages.

Then run your test again. What is the output?
 
must Janik
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well..Test passed. I don't know what `equal` and hashCode` changed...
 
Jeanne Boyarsky
author & internet detective
Posts: 38918
686
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do . Mockito uses the Hamcrest matchers. When you say which objects you expect to pass in as parameters, it calls equals() on the object ot determine if they are equivalent.

Before, you were using Object equals() implementation. Which checked if they were the same object. Since they weren't, Mockito didn't recognize them as being the same. Once you added the equals() implementation, Mockito saw them as the same and the tests behaved as you expected.
 
must Janik
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well... Thanks. What about the getTasks method? What should I change?
 
must Janik
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again, to be clear:

At 3 line I create new object of user. At 4 line I pass another object. They are not the same, right? How does it know that object from line that I pass and object from 4 line are the same... By this method?
 
Jeanne Boyarsky
author & internet detective
Posts: 38918
686
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It calls the equals method. Which checks that the username/password are the same.
 
must Janik
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh.. Okey. What about getTasks?
In my opinion, that's only bad implemented line. (Whole method in the first post)
 
Jeanne Boyarsky
author & internet detective
Posts: 38918
686
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getTasks() takes a user parameter so it had the same problem/solution.
 
must Janik
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:getTasks() takes a user parameter so it had the same problem/solution.


Well, it doesn't work anyway.
Here is my stacktrace:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!