GeezerVison is showing github.com — Add default recursive comparison configuration for non accessible classes and fields · Issue #3369 · assertj/assertj
Add default recursive comparison configuration for non accessible classes and fields #3369DescriptionFeature summary
At this moment I do not think AssertJ supports non-accessible classes and fields with Java 17+. Would it be useful to have a default RecursiveComparisonConfiguration that ignores all non exported packages, and non-accessible fields?
Example
public class ClassUnderTest {
private final int foo;
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public ClassUnderTest(int foo) {
this.foo = foo;
}
public static ClassUnderTest classUnderTest() {
return new ClassUnderTest(42);
}
}
And the test class as follows, note the configuration for the recursive comparison.
class ClassUnderTestTest {
@Test
void testClassUnderTest() {
assertThat(ClassUnderTest.classUnderTest())
.usingRecursiveComparison().ignoreNonExportedPackages()
.isEqualTo(new ClassUnderTest(42));
}
}
In the above, I'm only interested in the fact that I correctly call the constructor with value 42. Suppose this class is part of a library, then I'm unable to add an equals() and hashcode().
Without the call to (the proposed method) .ignoreNonExportedPackages() the test would fail. Currently, I would have to write .ignoringFieldsOfTypes(ReentrantReadWriteLock.class). In general I would need to ignore much more types and fields, since those would always cause errors in my test. Similar to non exported packages, one could also add a configuration; .ignoreNonAccessibleFields().
I'm interested to hear your opinion. Activityjoel-costigliola commented on Feb 19, 2024
At this moment I do not think AssertJ supports non-accessible classes and fields with Java 17+.
We know AssertJ can't introspect Java base types so it compares them with equals, this is not unreasonable since at some point you have to compare something.
Here are a couple ways of getting around your problem
Would that help ?
There is also the option of adding specific logic to certain type, for example the recursive comparison does not introspect Optional but simply get their values and compare them. If that makes sense for other java types we could do that. Meijuh commented on Feb 20, 2024
Would that help ?
That would solve my problem, but:
- I would have to always look into the deep insides of code I may know nothing about,
- do this whenever I write a recursive comparison.
Maybe my example above is not perfect. In a production example I get the following errors if I include comparing ReentrantReadWriteLock.class:
field/property 'httpClient.cookieStore.lock' differ:
- actual value : java.util.concurrent.locks.ReentrantReadWriteLock@7c88d04f[Write locks = 0, Read locks = 0]
- expected value: java.util.concurrent.locks.ReentrantReadWriteLock@71df5f30[Write locks = 0, Read locks = 0]
I guess above happens due to #2450, which will always give me a false positive?
Another example of a field I can not access:
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.concurrent.locks.Condition java.util.concurrent.ThreadPoolExecutor.termination accessible: module java.base does not "opens java.util.concurrent" to unnamed module @53ce1329
Above two errors gives me reason to believe I will not be able to do a full equivalence check anyway. So might as well ignore fields of particular types always.
So my suggestion is .ignoreNonExportedPackages() should always contain packages with such classes, instead of writing comparingOnlyFields or comparingOnlyFieldsOfTypes in all my assertions and in all my code bases. joel-costigliola commented on Feb 21, 2024 I'm not opposed to the principle of having ignoreNonExportedPackages although I don't think there are so many problematic types, most of them would have an overridden equals.
What might be unclear for users is the content of all these non exported packages, would there be cases where is makes sense to compare them with their equals method ?
I'm happy to get a contribution on this one if you feel like it. joel-costigliola commented on Mar 2, 2024 @Meijuh do you have a clear definition of "non exported" packages?
I'm asking for two reasons, first to guide the implementation (is there is an obvious way to detect that) and the documentation joel-costigliola commented on Mar 2, 2024 I'm leaning towards adding two options to the recursive comparison:
ignoringTypesMatchingRegexes(string... regexes) so that one can discard a bunch of types in packages
usingEqualsForTypesMatchingRegexes(string... regexes) similar to the previous but instead of ignoring we would use equals to compare these values
joel-costigliola commented on Mar 31, 2024 I have added ignoringFieldsOfTypesMatchingRegexes(String... regexes) but not yet ignoringFieldsOfTypesMatchingRegexes since I think the case we are trying to solve is for java types that can't be introspected and the recursive comparison already uses equals for them. added a commit that references this issue on Apr 1, 2024This issue will close once commit 8a7843e is merged into the 'main' branch.8a7843e
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|