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

  • SEARCH
  • Home
  • 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 6248937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:08:57+00:00 2026-05-24T13:08:57+00:00

I have a bunch of methods returning HashSet. I would like my unit test

  • 0

I have a bunch of methods returning HashSet. I would like my unit test to check the state of these objects i.e confirm that someObject.getName() == "foobar".

However the hashset iterator order is not guaranteed so my unit tests fail some times. How do I write unit tests for something like this?

Eg:

@Test
    public void testRowsToBeRead(){
        HashSet<SomeObject> rows = new SomeObject().read();
        assertEquals(19, rows.size());

        for(SomeObject r:rows){
            //How do I confirm contents?
        }
    }

I think I might have accepted an answer too soon.

The problem I now have is that I have implemented the equals method, which checks only for 2 fields in the object per design (it is mimicking a DB table).
However in my unit test, I want to check for all fields like description etc which is not in my equals. So if 2 of fields get swapped and these fields are not in my equals implementation, then the unit test gives a false positive.

  • 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-24T13:08:58+00:00Added an answer on May 24, 2026 at 1:08 pm

    My approach:

    public void testRowsToBeRead(){
        HashSet<SomeObject> expectedRows = new HasSet<SomeObject();
        expectedRows.add(new SomeObject("abc"));
        expectedRows.add(new SomeObject("def"));
    
        HashSet<SomeObject> rows = new SomeObject().read();
    
        // alternative 1
        assertEquals(19, rows.size());
    
        for(SomeObject r:rows){
            if (!expectedRows.contains(r)) {
                // test failed
            }
        }
    
        // alternative 2
        assertTrue(expectedRows.equals(rows));
    }
    

    To rely on this test you may need other unit tests confirming that SomeObject‘s equals and hashCode methods work as they should be…

    EDIT based on one of your comments

    If you want to check for fields not part of the equals contract, you have to iterate through the set:

    public void testRowsToBeRead(){
        HashSet<SomeObject> expectedRows = new HasSet<SomeObject();
        expectedRows.add(new SomeObject("a", "a1"));
        expectedRows.add(new SomeObject("b", "b1"));
    
        HashSet<SomeObject> rows = new SomeObject().read();
    
        for(SomeObject r : rows) {
            SomeObject expected = expectedRows.get(r); // equals and hashCode must still match
    
            if (expected == null) {
                // failed
            }
    
            if (!expected.getField1().equals(r.getField1()) && !expected.getField2().equals(r.getField2())) {
                // failed
            }
        }
    }
    

    In the above example SomeObject‘s equals may look like this, it only checks for field1:

    @Override
    public boolean equals(Object other) {
        return this.getField1().equals( ((SomeObject) other).getField1() );
    }
    

    No question, depending on your concrete use case this may become more complex. Hope this helps…

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

Sidebar

Related Questions

I have a bunch of methods that look like these two: public void SourceInfo_Get()
I have a helper class that is just a bunch of static methods and
I have a bunch a values I would like to add together which are
I have a DAL that is composed of a bunch of methods that perform
I have a bunch of different methods that are not supposed to run concurrently,
I have a bunch of connection related methods that I need to execute before
I have a bunch of legacy documents that are HTML-like. As in, they look
I have a bunch of repository classes which I want to unit-test using Visual
I have a bunch of methods which I want to check some metadata of,
I have a bunch of files that I need to be able to transport

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.