{"record":{"id":"3b7a9b52abe71fbd","repo":"apache/beam","slug":"if-you-meant-to-test-object-equality-use-containsinanyorder","errorCode":null,"errorMessage":"If you meant to test object equality, use .containsInAnyOrder instead.","messagePattern":"If you meant to test object equality, use \\.containsInAnyOrder instead\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/testing/PAssert.java","lineNumber":803,"sourceCode":"      })\n      SerializableFunction<Iterable<T>, Void> checkerFn =\n          (SerializableFunction) new MatcherCheckerFn<>(matcher);\n      actual.apply(\n          \"PAssert$\" + assertCount++,\n          new GroupThenAssert<>(checkerFn, rewindowingStrategy, paneExtractor, site));\n      return this;\n    }\n\n    /**\n     * @throws UnsupportedOperationException always\n     * @deprecated {@link Object#equals(Object)} is not supported on PAssert objects. If you meant\n     *     to test object equality, use a variant of {@link #containsInAnyOrder} instead.\n     */\n    @Deprecated\n    @Override\n    @SuppressFBWarnings(\"EQ_UNUSUAL\")\n    public boolean equals(@Nullable Object o) {\n      throw new UnsupportedOperationException(\n          \"If you meant to test object equality, use .containsInAnyOrder instead.\");\n    }\n\n    /**\n     * @throws UnsupportedOperationException always.\n     * @deprecated {@link Object#hashCode()} is not supported on PAssert objects.\n     */\n    @Deprecated\n    @Override\n    public int hashCode() {\n      throw new UnsupportedOperationException(\n          String.format(\"%s.hashCode() is not supported.\", IterableAssert.class.getSimpleName()));\n    }\n  }\n\n  /**\n   * An {@link IterableAssert} for an iterable that is the sole element of a {@link PCollection}.\n   * This does not require the runner to support side inputs.","sourceCodeStart":785,"sourceCodeEnd":821,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/PAssert.java#L785-L821","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Replace equals-based checks with content assertions: use containsInAnyOrder(...) or isInAnyOrder(...) on the assertion","Compare the underlying PCollection values via a satisfies(...) checker rather than comparing assert objects","If using a collection helper that calls equals (contains, isIn), switch to the PAssert-supported variant like containsInAnyOrder"],"exampleFix":"// before\nassertThat(pcollection).isIn(Collections.singletonList(expectedElem));\n// after\nassertThat(pcollection).containsInAnyOrder(expectedElem);","handlingStrategy":"validation","validationCode":"if (obj instanceof org.apache.beam.sdk.testing.PAssert.PCollectionContentsAssert) {\n  throw new IllegalStateException(\"Use containsInAnyOrder(...) on the PAssert, not equals-based APIs\");\n}","typeGuard":"boolean isPAssertObject(Object o) {\n  return o instanceof org.apache.beam.sdk.testing.PAssert.IterableAssert;\n}","tryCatchPattern":"try {\n  equalityCheck(pAssertObject, expected);\n} catch (UnsupportedOperationException e) {\n  if (e.getMessage().contains(\"containsInAnyOrder\")) {\n    // rewrite assertion to content-based check\n  }\n}","preventionTips":["Never pass PAssert assertion objects to assertEquals/contains/isIn or Set APIs","Always assert PCollection contents via PAssert methods (containsInAnyOrder, isEqualTo, satisfies)","Remember PAssert objects are assertion builders, not values — keep them out of collections"],"tags":["apache-beam","passert","equals-contract","testing"],"backgroundTag":"unsupported-operation","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}