Comments (6)
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
- select the fields to compare with comparingOnlyFields or comparingOnlyFieldsOfTypes
- registering a bi-predicate to compare certain fields or types
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.
from assertj.
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.
from assertj.
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.
from assertj.
@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
from assertj.
I'm leaning towards adding two options to the recursive comparison:
ignoringTypesMatchingRegexes(string... regexes)
so that one can discard a bunch of types in packagesusingEqualsForTypesMatchingRegexes(string... regexes)
similar to the previous but instead of ignoring we would useequals
to compare these values
from assertj.
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.
from assertj.
Related Issues (20)
- Standardize ShouldBeEqual and remove AssertionErrorFactory HOT 2
- Failed containsOnlyNulls Assertion for Null-Extracted List Elements HOT 4
- Missing consistent order of Javadoc tags
- Merge assertj-core-java17 into assertj-core HOT 3
- Generating Javadoc in a Maven project using <detectLinks> does not work HOT 5
- Navigation to `assertj-core` or `guava` types from `assertj-guava` Javadoc site has unnecessary header
- `satisfiesOnlyOnce` stack trace does not point to failed nested assertion HOT 3
- Evaluate `DateTimeFormatter` as replacement of `SimpleDateFormat` for internal usage
- Support for Duration in Assertions.byLessThan, Assertions.within HOT 2
- Add an unwrap method to the API to get actual value HOT 3
- Unable to construct iterable assertions across a specific type easily HOT 7
- Ambiguous `assertThat` for `Temporal` instance implementing `Comparable` HOT 3
- 3.26 - Ambiguous assertThat with Temporal implementation HOT 1
- Extra variant for doesNotMatch
- Add an annotation Class navigation method
- Recursive Comparison With Ignored Fields Fails On Equal Collections HOT 4
- hasFieldOrPropertyWithValue accepting arrays
- reference to assertThat is ambiguous when asserting a `java.time.Year` HOT 1
- JPMS assertions HOT 2
- Do we have any way to describe the expected value? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from assertj.