Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
AssertEquals when list content is unordered

How would you refactor the following if the products can be returned in any order?

List<Product> products = get_products("test_produc");
assertEquals(products.size(),3);
assertEquals(products.get(0).getName(), "test_product1");
assertEquals(products.get(1).getName(), "test_product2");
assertEquals(products.get(2).getName(), "test_produc3");

If it can be done elegantly using streams then I'm oopen to such suggestions. Hamcrest suggestions are also welcome.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Note that assertEquals also works on Lists and Sets directly. This is much less typing and it will give very clear error messages.

If the return values are not allowed to contain duplicates, they should return a Set instead of a List. If you can change the function you are testing is this way you can test it as follows:

assertEquals(new HashSet<>(Arrays.asList("Item1", "Item2")), get_products());

If this is not an option you should sort both the expected and the actual results and compare those:

asssertEquals(Arrays.sort(Arrays.asList("Item1", "Item2")), Arrays.sort(get_products()));

Finally you could resort to using Hamcrest matchers (the function containsInAnyOrder is in org.hamcrest.collection.IsIterableContainingInAnyOrder):

assertThat(get_products(), containsInAnyOrder("Item1", "Item2"));
about 4 years ago · Santiago Trujillo Report

0

I would recommend using AssertJ -

import static org.assertj.core.api.Assertions.assertThat;

// ......

List<String> products = get_products("test_produc").stream()
    .map(Product::getName)
    .collect(toList());

assertThat(products).containsExactlyInAnyOrder("Third", "Second", "First");

which additionally gives you many more fluent assertions (especially the exception handling ones) as well.

about 4 years ago · Santiago Trujillo Report

0

I'd combine the answer of Abubakkar with Hamcrest compare collections

List<String> productNames = products.stream()
                                    .map(p -> p.getName())
                                    .collect(Collectors.toList());
// assert that the actual list does not contain additional elements:
assertEquals(products.size(),3);
assertThat(productNames, containsInAnyOrder("test_product1", "test_product2", "test_produc3"));
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!