How do I assert equality on two classes without an equals method?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In object-oriented programming, accurately comparing objects often requires implementing an equals method within a class. However, there are scenarios where you need to assert the equality of two objects without altering their classes, perhaps due to third-party restrictions or architectural constraints. In such cases, you must employ alternative approaches to ensure you can assert equality effectively.
Object Equality Basics
Before diving into the alternatives, it's essential to understand what equality means for objects:
- Reference Equality: This checks whether two references point to the exact same object in memory. In Java, this is done using the
==operator. - Structural Equality: Determines if two objects are equivalent in terms of their contents or state, usually evaluated via the
equalsmethod.
In scenarios where you don't have access to the .equals method, or it's not overridden to meet custom equality requirements, you’ll need other techniques to assert equality.
Approaches to Asserting Equality Without equals
1. Use Reflection
When you cannot alter a class, reflection is a powerful feature to consider. It allows you to inspect and manipulate objects at runtime:
Advantages:
- Versatility: Can be used without modifying class definitions.
- Powerful: Can access private fields which are not usually available.
Disadvantages:
- Performance: Reflection can be slower and can affect performance especially for large objects or frequent operations.
- Security: Could potentially violate encapsulation principles.
2. Use External Libraries
Libraries like Apache Commons Lang provide utilities for comparing fields without defining an equals method.
Example using Apache Commons:
Advantages:
- Simplicity: Reduces boilerplate code.
- Community Support: Well-tested, with community support.
Disadvantages:
- Dependency: Adds additional dependencies to your project.
3. Write a Custom Comparator
Custom comparators give you the flexibility to define what 'equality' means without modifying the classes involved.
Example:
Advantages:
- Clarity: Explicitly states the criteria for equality.
- Non-Intrusive: Leaves original class definition unchanged.
Disadvantages:
- Complexity: Could become cumbersome if many fields need to be compared.
Summary Table
| Approach | Advantages | Disadvantages |
| Reflection | Versatile, Accesses Privates | Performance, Security |
| External Libraries | Simple, Community-Supported | Adds Dependencies |
| Custom Comparator | Clear, Non-Intrusive | Potentially Complex |
Additional Considerations
- Handling Immutable Objects
For immutable objects, any of the above approaches work well, but performance considerations tend to be less significant because the state does not change, so caching reflection results might be effective.
- Testing Strategies
When asserting equality in tests:
- Assertions Frameworks: Consider using frameworks like JUnit Assert or AssertJ, which facilitate complex assertions.
- Coverage: Ensure that edge cases (e.g., null values or class hierarchies) are covered.
Conclusion
While the absence of an equals method complicates matters, suitable alternatives exist for asserting object equality effectively. Each method brings its trade-offs related to performance, simplicity, and architectural impact. Understanding these factors can guide you to the most appropriate solution for your specific use case.
Related reading
- How do I assert my exception message with JUnit Test annotation?
- How do I calculate someone's age in Java?
- How do I change log level in runtime without restarting Spring Boot application
- How do i change logback version in spring boot?
- How do I change the IntelliJ IDEA default JDK?
- How do I check if a file exists in Java?
- How do I check if my EditText fields are empty?
- How do I check if the Java JDK is installed on Mac?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.