• 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

[JUnit] Testing only few fields of two POJOs in Junit using Mockito

 
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
I am testing two Pojos as follows



but what I am trying to find is not to mention fields that I want to exclude but fields which I want to include. Is there any way ??

 
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 smell a test smell; possibly a design smell, too. What's your motivation for using ReflectionEquals?
 
author & internet detective
Posts: 41125
848
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 agree with Junilu.

Also, if you know the fields you want to compare, why not just call their getters?
 
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
Task : I am  converting external request to Service to Internal Command.  So essentially I need to test the quality of two POJOs  and I need to test the quality for few fields only.

I agree I can write getters but they are many.

There is another option to use Matchers SamePropertyValueAs , but it does not provide any fields to include or exclude. This was the motivation to switch to ReflectionEquals.
 
Junilu Lacar
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 still don't get what you really need to do. You are explaining in terms of implementation details, we are asking about intent.

It's the difference between saying "I want to iterate over an array and add each value to variable x" vs. "I want to calculate an average speed flown during all runs."

You are telling us the former while we are looking to understand the latter.
 
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
So intent is to check whether transformation of one Pojo to another pojo is done correctly. Essentially I am testing static transformer method which converts one pojo to another.
 
Junilu Lacar
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
So, if you only care about certain fields that you want to explicitly list out, why use reflection?  That's the smell. Using reflection to exclude specific fields makes sense because you won't have to change the code if that exclusion list doesn't change but the objects are modified to include more fields that should be checked. In your case, it doesn't make sense to use reflection because you want to list fields to include.  If that list changes, you'll still have to change the code. You gain nothing by using reflection. Since reflection is costly, you're paying for a service that is of no practical use to you in the context of the problem you're trying to solve. That's like paying for premium cable channels that you don't ever watch.  So what's the point?
 
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

Junilu Lacar wrote:So, if you only care about certain fields that you want to explicitly list out, why use reflection?  That's the smell. Using reflection to exclude specific fields makes sense because you won't have to change the code if that exclusion list doesn't change but the objects are modified to include more fields that should be checked. In your case, it doesn't make sense to use reflection because you want to list fields to include.  If that list changes, you'll still have to change the code. You gain nothing by using reflection. Since reflection is costly, you're paying for a service that is of no practical use to you in the context of the problem you're trying to solve. That's like paying for premium cable channels that you don't ever watch.  So what's the point?



Got your Point, thanks a bunch ! and I think for the same reason they don't have include fields in api but exclude fields only.
 
pie. 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