apache/beam · error · UnsupportedOperationException

If you meant to test object equality, use…

Error message

If you meant to test object equality, use .containsInAnyOrder instead.

What it means

PAssert's PCollectionContentsAssert (IterableAssert) intentionally breaks Object.equals: calling equals() always throws UnsupportedOperationException with this message. This is a deliberate guard so tests fail loudly instead of silently comparing assert-object references; users should test PCollection contents via assertion methods like containsInAnyOrder.

Solutions

  1. Replace equals-based checks with content assertions: use containsInAnyOrder(...) or isInAnyOrder(...) on the assertion
  2. Compare the underlying PCollection values via a satisfies(...) checker rather than comparing assert objects
  3. If using a collection helper that calls equals (contains, isIn), switch to the PAssert-supported variant like containsInAnyOrder

Example fix

// before
assertThat(pcollection).isIn(Collections.singletonList(expectedElem));
// after
assertThat(pcollection).containsInAnyOrder(expectedElem);
Defensive patterns

Strategy: validation

Validate before calling

if (obj instanceof org.apache.beam.sdk.testing.PAssert.PCollectionContentsAssert) {
  throw new IllegalStateException("Use containsInAnyOrder(...) on the PAssert, not equals-based APIs");
}

Type guard

boolean isPAssertObject(Object o) {
  return o instanceof org.apache.beam.sdk.testing.PAssert.IterableAssert;
}

Try / catch

try {
  equalityCheck(pAssertObject, expected);
} catch (UnsupportedOperationException e) {
  if (e.getMessage().contains("containsInAnyOrder")) {
    // rewrite assertion to content-based check
  }
}

Prevention

When it happens

Trigger: Invoking equals() on a PAssert assertion object, most commonly through collection APIs: assertThat(...).isIn(list), assertEquals(assertObj, other), assertThat(listOfAsserts).contains(assertObj), or Stream.distinct()/Set membership involving PAssert objects.

Common situations: Placing PAssert assert objects in collections or sets, using assertThat() from a non-Beam assertion library that resolves to equals, or copying test code that mixed up assert objects with PCollections.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/3b7a9b52abe71fbd. Report an issue: GitHub.

Appendix: source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/testing/PAssert.java:803

      })
      SerializableFunction<Iterable<T>, Void> checkerFn =
          (SerializableFunction) new MatcherCheckerFn<>(matcher);
      actual.apply(
          "PAssert$" + assertCount++,
          new GroupThenAssert<>(checkerFn, rewindowingStrategy, paneExtractor, site));
      return this;
    }

    /**
     * @throws UnsupportedOperationException always
     * @deprecated {@link Object#equals(Object)} is not supported on PAssert objects. If you meant
     *     to test object equality, use a variant of {@link #containsInAnyOrder} instead.
     */
    @Deprecated
    @Override
    @SuppressFBWarnings("EQ_UNUSUAL")
    public boolean equals(@Nullable Object o) {
      throw new UnsupportedOperationException(
          "If you meant to test object equality, use .containsInAnyOrder instead.");
    }

    /**
     * @throws UnsupportedOperationException always.
     * @deprecated {@link Object#hashCode()} is not supported on PAssert objects.
     */
    @Deprecated
    @Override
    public int hashCode() {
      throw new UnsupportedOperationException(
          String.format("%s.hashCode() is not supported.", IterableAssert.class.getSimpleName()));
    }
  }

  /**
   * An {@link IterableAssert} for an iterable that is the sole element of a {@link PCollection}.
   * This does not require the runner to support side inputs.

View on GitHub (pinned to 12126d8942)