Skip to content

Unstable behavior in PredictRating() unit test due to constructor field shadowing #543

@ngtduc693

Description

@ngtduc693

Describe the bug

The unit test PredictRating_WithOtherUserHavingRatedTargetItem_ShouldCalculateSimilarityAndWeightedSum occasionally fails even though the logic and expected values are correct. The behavior is non-deterministic — it sometimes passes, sometimes fails.

To Reproduce

  • Run the test suite multiple times (Run ALL unit tests, not only it)
  • Observe that PredictRating_WithOtherUserHavingRatedTargetItem_ShouldCalculateSimilarityAndWeightedSum sometimes fails randomly

Expected behavior

  • The field should consistently reference the injected dependency.
  • The test should deterministically pass when the mock returns fixed values.

Actual behavior

  • Test passes intermittently.
  • When the mock reference is lost (null), similarity calculations fall back to undefined behavior.

Root cause

This test class used fixture-level fields (e.g., the ISimilarityCalculator mock, the CollaborativeFiltering instance, and the ratings data) that were re-initialized in [SetUp]. When the whole test suite runs (often in parallel), another test’s [SetUp] can overwrite those fields while this test is executing

=> Shared mutable test state + parallel execution = race condition.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions