Skip to content

extracting(String) eats all exceptions thrown by property getter in certain cases #3868

Description

@doublep

Describe the bug
When using ...extracting ("foo"), any exceptions thrown by property getter (i.e. getFoo()) is silently ignored if there is also a field named foo.

  • assertj core version: 3.27.3
  • java version: 27
  • test framework version: verified with AssertJ called directly (without JUnit or something else)
  • os (if relevant):

Test case reproducing the bug

Add a test case showing the bug that we can run

    public static void main (String[] args)
    {
        assertThat (new Bean ()).extracting ("computed").isNull ();  // This passes, even though getComputed() throws
        assertThat (new Bean ().getComputed ()).isNull ();           // But this fails as expected
    }

    public static class Bean
    {
        private String  computed;

        public String getComputed ()
        {
            if (computed == null)  // Typical "compute once, then return the already computed value" pattern
                computed = doCompute ();

            return computed;
        }

        protected String doCompute ()
        {
            throw new RuntimeException ("failed to compute");  // But computation is buggy and throws an exception
        }
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions