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 6200661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:24:37+00:00 2026-05-24T04:24:37+00:00

In my unit test class, I am having two list. One is the expectedValue

  • 0

In my unit test class, I am having two list. One is the expectedValue and other is returnedType.

I am doing

Collections.sort(expected);
Collections.sort(returned);

but how do I compare two list based on the type of value of one of its element? For example I have element sortOrder in both the list and it has values from 1,2 or 3 so how do i compare or say doing something like assertEqual(expected, returned) for both the list and make sure that both list has same elements and has same sortOrder meanings elements are also in proper sort format?

Note: I should not be using any external libraries for doing it.

Update

Example of my return and expected list:

excepted List = [Code: ANALYST, SortOrder: 2, Code: STREET, SortOrder: 1] and
returned List = [Code: STREET, SortOrder: 1, Code: ANALYST, SortOrder: 2]

So at very basic question here is how can I sort a list based on one of its element value, in our example according to sortOrder value so our expected should be

excepted List = [Code: STREET, SortOrder: 1, Code: ANALYST, SortOrder: 2]

  • 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-24T04:24:38+00:00Added an answer on May 24, 2026 at 4:24 am

    Edit:

    Best way is to do

    assertEquals(expected, returned);
    

    because AbstractList implements equals using pairwise comparison.
    If you are not sure, the Lists you are receiving inherit from AbstractList, you can always do

    assertEquals(new ArrayList(expected), new ArrayList(returned));
    

    I leave the rest of the answer for documentary purposes (and to not hide my shameful ignorance)

    Do you want to assert that the lists are in the correct order also?

    public static void assertEquals(List expected, List returned)
    {
        assertEquals(expected.size(), returned.size());
        if(!expected.containsAll(returned))
        {
           fail();
        }
    
        for(int i = 0; i < expected.size(); i++)
        {
            assertEquals(expected.get(i),returned.get(i));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One of my unit tests seems to randomly pass or fail when I run
I am having an problem writing unit tests for an simple session wrapper. The
I'm having some trouble with changing the value of a class at runtime and
I am writing unit tests for objects that are cloned, serialized, and/or written to
Possible Duplicate: Hard-Coded Mock Objects vs Mocking Framework I think I'm finally beginning to
I am receiving the following error when attempting to inject my dao into a
I have SampleClass.java with 5 methods in package /MyProject/src/sample and I use Eclipse to
I am attempting to use the Struts2 JUnit Plugin (v2.2.3 w/ Struts2 v2.2.3) and
Using Linq to sql and server explorer, I mapped to a loginvalidation stored proc.
I'm slowly making my way through the rails source, to get a better grip

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.