Java Test Automation / Quiz
Identity Traps in Assertions and Sets
Diagnose value objects that compare correctly in one assertion but behave unpredictably in hashed collections.
- Difficulty
- Medium
- Format
- Quiz
- Points
- 150
- Estimate
- 8 min
// MISSION BRIEF
Your Mission
Two test-case identifiers look equal, yet a deduplication set retains both and later loses track of a mutated entry. Audit the value-object methods and apply the contracts Java collections depend on.
// FIRST CONTACT
Battle teaser
Review this class used in assertions and HashSet: final class CaseId { private final String value; CaseId(String value) { this.value = value; } @Override public boolean equals(Object other) { return other instanceof CaseId id && value.equals(id.value); } } What defect remains?
- AHashSet requires CaseId to extend ArrayList
- BIt overrides equals but not hashCode, violating the rule that equal objects must produce the same hash code
- CThe final keyword prevents equality checks
- Dequals must compare objects only with == to work in a HashSet
Answers, scoring, hints, and the full battle stay sealed.
// SKILL TAGS
java-equalityhashcodehashsetjava-testing