• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Testing Equality of Two Objects of differewnt class.

 
Ranch Hand
Posts: 162
1
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use Case :  I am converting external model for service to internal domain model.  Let say External model name is E and Internal Domain model name is I.

Query  : Now I am want to test the I model is successfully build from E in Unit test.  For that I need to assert for all the fields in I and E respectively as follows




Is there any Library or built in api that can solve this to one line ?
 
Saloon Keeper
Posts: 14010
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your external type follows the JavaBeans conventions, you can use an existing Hamcrest matcher. Sadly, the matcher is type-safe, so it's easiest to wrap it in a method call that takes all kinds of objects:

You then call it like this:
 
Sangel Kapoor
Ranch Hand
Posts: 162
1
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:If your external type follows the JavaBeans conventions, you can use an existing Hamcrest matcher. Sadly, the matcher is type-safe, so it's easiest to wrap it in a method call that takes all kinds of objects:

You then call it like this:



This is not working for me , error :  incompatible type. I think Matcher<Object> and type of internal are different thats why it is coming. Any suggestions ?
 
Marshal
Posts: 17008
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would try casting internal to Object in the assertThat call.
 
Stephan van Hulst
Saloon Keeper
Posts: 14010
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That should not be necessary.

assertThat() takes a T and a Matcher<? super T>, so regardless of the formal type of internal, Matcher<Object> is compatible.

Can you post the full stack trace?
 
Sangel Kapoor
Ranch Hand
Posts: 162
1
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
He baked a muffin that stole my car! And this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic