GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #3369

New issue
Copy link
New issue
Copy link
Closed
Closed
Add default recursive comparison configuration for non accessible classes and fields#3369
Copy link
Assignees
joel-costigliola
Labels
theme: recursive comparisonAn issue related to the recursive comparison
Milestone
3.26.0

Description

@Meijuh
Meijuh
opened on Feb 19, 2024
Issue body actions

Feature 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.

Activity

  1. scordio
    added
    theme: recursive comparisonAn issue related to the recursive comparison
    on Feb 19, 2024
  2. joel-costigliola commented on Feb 19, 2024

    @joel-costigliola
    joel-costigliola
    on Feb 19, 2024
    Member
    More actions

    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.

  3. Meijuh commented on Feb 20, 2024

    @Meijuh
    Meijuh
    on Feb 20, 2024
    Author
    More actions

    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.

  4. joel-costigliola commented on Feb 21, 2024

    @joel-costigliola
    joel-costigliola
    on Feb 21, 2024
    Member
    More actions

    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.

  5. joel-costigliola
    added this to the 3.26.0 milestone on Feb 26, 2024
  6. joel-costigliola commented on Mar 2, 2024

    @joel-costigliola
    joel-costigliola
    on Mar 2, 2024
    Member
    More actions

    @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

  7. joel-costigliola commented on Mar 2, 2024

    @joel-costigliola
    joel-costigliola
    on Mar 2, 2024
    Member
    More actions

    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
  8. joel-costigliola
    self-assigned this
    on Mar 17, 2024
  9. joel-costigliola
    closed this as completedin 8a7843eon Mar 31, 2024
  10. joel-costigliola commented on Mar 31, 2024

    @joel-costigliola
    joel-costigliola
    on Mar 31, 2024
    Member
    More actions

    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.

  11. Banuelorigni
    added a commit that references this issue on Apr 1, 2024

    Add ignoringFieldsOfTypesMatchingRegexes

    This issue will close once commit 8a7843e is merged into the 'main' branch.
    8a7843e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @joel-costigliola
    joel-costigliola

Labels

theme: recursive comparisonAn issue related to the recursive comparison

Type

No type

Projects

No projects

    Milestone

    • 3.26.0

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.