Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 366631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:39:04+00:00 2026-05-12T13:39:04+00:00

Say I have a unit test that wants to compare two complex for objects

  • 0

Say I have a unit test that wants to compare two complex for objects for equality. The objects contains many other deeply nested objects. All of the objects’ classes have correctly defined equals() methods.

This isn’t difficult:

@Test
public void objectEquality() {
    Object o1 = ...
    Object o2 = ...

    assertEquals(o1, o2);
}

Trouble is, if the objects are not equal, all you get is a fail, with no indication of which part of the object graph didn’t match. Debugging this can be painful and frustrating.

My current approach is to make sure everything implements toString(), and then compare for equality like this:

    assertEquals(o1.toString(), o2.toString());

This makes it easier to track down test failures, since IDEs like Eclipse have a special visual comparator for displaying string differences in failed tests. Essentially, the object graphs are represented textually, so you can see where the difference is. As long as toString() is well written, it works great.

It’s all a bit clumsy, though. Sometimes you want to design toString() for other purposes, like logging, maybe you only want to render some of the objects fields rather than all of them, or maybe toString() isn’t defined at all, and so on.

I’m looking for ideas for a better way of comparing complex object graphs. Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:39:04+00:00Added an answer on May 12, 2026 at 1:39 pm

    What you could do is render each object to XML using XStream, and then use XMLUnit to perform a comparison on the XML. If they differ, then you’ll get the contextual information (in the form of an XPath, IIRC) telling you where the objects differ.

    e.g. from the XMLUnit doc:

    Comparing test xml to control xml [different] 
    Expected element tag name 'uuid' but was 'localId' - 
    comparing <uuid...> at /msg[1]/uuid[1] to <localId...> at /msg[1]/localId[1]
    

    Note the XPath indicating the location of the differing elements.

    Probably not fast, but that may not be an issue for unit tests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 194k
  • Answers 194k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try the following: using (SmartSqlReader reader = Db.CurrentDb.ExecuteReader(sp)) { while… May 12, 2026 at 6:41 pm
  • Editorial Team
    Editorial Team added an answer NUnit provides the Values attribute which can be used together… May 12, 2026 at 6:41 pm
  • Editorial Team
    Editorial Team added an answer Actually, I found the solution here. public const int WM_NCLBUTTONDOWN… May 12, 2026 at 6:41 pm

Related Questions

I am not sure what I should be doing here. Should I be hardcoding
I'm a software test engineer embedded in a development team. A large part of
I have a pseudo random number generator (PRNG) class that I want to unit
I am working on a lib that I want to release in open source.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.