What does TestNG's Assert.assertEqualsNoOrder check, and how does it treat duplicate elements and nested arrays?
answer
- order ignored, contents not
- sizes compared first
- each duplicate counted
- elements compared with their own equals
basics
~10 sTestNG's Assert.assertEqualsNoOrder passes when two arrays, collections or iterators hold the same elements in any order. Sizes must match and each duplicate must appear equally often; nested arrays are compared by reference, not deeply.
solid answer
~40 s`Assert.assertEqualsNoOrder(actual, expected)` has overloads for `Object[]`, `Collection<?>` and `Iterator<?>`. It fails first if the sizes differ, then removes each expected element once from a copy of the actual elements and fails if anything is left over, so it compares like a multiset: `[a, b, b]` against `[a, a, b]` fails. Elements are matched with their own `equals`, so a nested array matches only the very same array object; the javadoc says plainly that arrays are not compared deeply. I use it for results whose order is not part of the contract, such as rows from an unordered query. Plain `assertEquals` on two lists checks order, and converting both sides to sets hides duplicate counts. TestNG 7.12.0 fixed a false pass in the collection overloads, so the version matters.
code
java · 20 linesimport static org.testng.Assert.assertEqualsNoOrder;
import static org.testng.Assert.expectThrows;
import java.util.List;
import org.testng.annotations.Test;
public class SearchResultsTest {
@Test
public void returnsMatchingSkusInAnyOrder() {
List<String> actual = List.of("SKU-3", "SKU-1", "SKU-2");
assertEqualsNoOrder(actual, List.of("SKU-1", "SKU-2", "SKU-3"));
}
@Test
public void duplicateCountsMustMatch() {
expectThrows(AssertionError.class,
() -> assertEqualsNoOrder(List.of("a", "b", "b"), List.of("a", "a", "b")));
}
}go deeper
Recall that assertEqualsNoOrder ignores order for arrays, collections and iterators, while assertEquals on two lists does not.
Explain the size check and cancel-one-by-one algorithm, why duplicates must match in count, and why arrays as elements compare by reference.
Judge when order truly is not part of the contract, check the element type's equals, and know that versions before 7.12.0 could pass falsely.
Decide whether tests should pin an order at the source, such as an explicit sort in the query, rather than relaxing the assertion with assertEqualsNoOrder.
## What it checks `org.testng.Assert.assertEqualsNoOrder` asserts that two groups of elements are equal **ignoring order**. In TestNG 7.12 it has overloads for three shapes, each with and without a trailing message: - `assertEqualsNoOrder(Object[] actual, Object[] expected)` - `assertEqualsNoOrder(Collection<?> actual, Collection<?> expected)` - `assertEqualsNoOrder(Iterator<?> actual, Iterator<?> expected)` As everywhere in `org.testng.Assert`, the actual value comes first. The typical use is a result whose order is not part of the contract: rows from a query without an ordering clause, items returned by a search that ranks ties arbitrarily, or the keys of a hash-based structure. ## How the comparison works The collection and iterator overloads, and the array overload in the same way, follow three steps: 1. **Compare sizes.** If the two groups have different sizes, the check fails at once with a message such as `Collections do not have the same size: 3 != 2`. 2. **Cancel matches.** Copy the actual elements into a list, then remove each expected element from that copy once, using the element's `equals`. 3. **Check the remainder.** If anything is left in the copy, some actual element had no partner, and the check fails with a message listing both groups. Because each expected element cancels exactly one actual element, the comparison behaves like a **multiset** (a bag): order is ignored, but how many times each value appears is not. ## Duplicates | actual | expected | Result | Why | |---|---|---|---| | `[b, a, c]` | `[a, b, c]` | Pass | Same elements, different order | | `[a, b, b]` | `[a, a, b]` | Fail | One `b` is left after cancelling | | `[a, b]` | `[a, b, b]` | Fail | Sizes differ | | `[a, a]` | `[a, a]` | Pass | Each `a` cancels one `a` | The second row is the one interviewers like: a candidate who thinks "ignore order" means "compare as sets" predicts a pass. ## Nested arrays and `equals` - Matching relies entirely on each element's **`equals` method**. A domain object without its own `equals` matches only itself, so two rows that print identically still fail. - An `equals` that ignores a field lets a wrong value in that field pass unnoticed. - **Arrays as elements are not compared deeply.** An array's `equals` is reference identity, so `new String[] {"x"}` inside the actual group never matches a separate `new String[] {"x"}` inside the expected group. TestNG's javadoc for the array overload states this explicitly. ## Choosing among the equality checks | You want to assert | Use | Watch out for | |---|---|---| | Same elements, same order | `assertEquals(list, list)` | Fails on a harmless reordering | | Same elements, any order, counts matter | `assertEqualsNoOrder` | Elements need a meaningful `equals` | | Same distinct values, counts irrelevant | Convert both to sets, then `assertEquals` | Duplicates disappear silently | ## The message parameter replaces, not prefixes `assertEquals` on two plain values puts your message in front of its own `expected [x] but found [y]` text. `assertEqualsNoOrder` behaves differently: when you pass a message, it is used **instead of** the default text, so the report loses the size mismatch or the listing of both groups. Either leave the message off or build the two groups into it yourself. ## Iterators are consumed The `Iterator` overload copies both iterators into lists before comparing, which **drains** them. After the call, `hasNext()` returns `false` on both, so a test that wants to inspect the same elements again needs the source collection, not the iterator. ## When order really is the contract Reach for `assertEqualsNoOrder` only when the specification genuinely leaves order open. If an endpoint promises results sorted by date, relaxing the check hides exactly the regression the test exists to catch; assert the order with `assertEquals` instead. And if order is unspecified only because the query forgot an ordering clause, fixing the query is often better than relaxing every test that reads it. ## Version note TestNG 7.12.0 fixed a false positive (GITHUB-3227): the collection and iterator overloads could pass on equal-sized inputs where the actual elements were a subset of the expected ones, such as `[a, b, b]` against `[a, b, c]`. On older versions, a green `assertEqualsNoOrder` over collections with repeated values deserves a second look.
- Why can a custom message make an assertEqualsNoOrder failure harder to debug?Because the message replaces TestNG's default text instead of being prefixed to it, so the report no longer shows the size mismatch or the two groups being compared. assertEquals on two plain values behaves differently and keeps its 'expected [x] but found [y]' text after your message. Leave the message off, or include both groups in it.
- Why does a correct-looking assertEqualsNoOrder still depend on your element class?Matching removes each expected element from a copy of the actual list with that element's equals. A class without equals matches only the same object, so equal-looking rows fail, and an equals that ignores a field lets a wrong value in it pass. Check the element type's equals before relying on the assertion.
- assertEqualsNoOrder compares the two groups as sets, so duplicate counts are ignored.
- assertEqualsNoOrder compares nested arrays element by element.
- Plain assertEquals on two lists already ignores element order.
- A custom message is added in front of the default assertEqualsNoOrder failure text.
- assertEqualsNoOrder needs the elements to be sortable to compare them.
Assumes TestNG 7.12.0, which fixed a false pass in the Collection and Iterator overloads of assertEqualsNoOrder on equal-sized inputs with repeated values.