Giter Site home page Giter Site logo

Comments (6)

joel-costigliola avatar joel-costigliola commented on July 24, 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.

from assertj.

Meijuh avatar Meijuh commented on July 24, 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.

from assertj.

joel-costigliola avatar joel-costigliola commented on July 24, 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.

from assertj.

joel-costigliola avatar joel-costigliola commented on July 24, 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

from assertj.

joel-costigliola avatar joel-costigliola commented on July 24, 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

from assertj.

joel-costigliola avatar joel-costigliola commented on July 24, 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.

from assertj.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.