Win a copy of High Performance Python for Data Analytics this week in the Python 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Should I unit test these method if so, how?

 
Ranch Hand
Posts: 122
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 40403
821
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 122
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 40403
821
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm saying you should write equals and hash code methods for this object.
 
must Janik
Ranch Hand
Posts: 122
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I generated these method. What should I do know?
 
Jeanne Boyarsky
author & internet detective
Posts: 40403
821
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 122
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well..Test passed. I don't know what `equal` and hashCode` changed...
 
Jeanne Boyarsky
author & internet detective
Posts: 40403
821
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 122
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well... Thanks. What about the getTasks method? What should I change?
 
must Janik
Ranch Hand
Posts: 122
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 40403
821
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It calls the equals method. Which checks that the username/password are the same.
 
must Janik
Ranch Hand
Posts: 122
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 40403
821
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getTasks() takes a user parameter so it had the same problem/solution.
 
must Janik
Ranch Hand
Posts: 122
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Don't listen to Steve. Just read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic