Skip to content

hasFieldOrPropertyWithValue swallows exceptions thrown by getters, and reports non-existent property instead #3563

@DidierLoiseau

Description

@DidierLoiseau

Describe the bug
If a getter throws an exception, it is not reported in the AssertionError from hasFieldOrPropertyWithValue()

  • assertj core version: 3.26.3
  • java version: 11
  • test framework version: junit 5.10.2

Test case reproducing the bug

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ThrowingGetterTest {
  @Test
  void test() {
    assertThat(new MyThrowingPojo())
        .hasFieldOrPropertyWithValue("property", "value");
  }

  static class MyThrowingPojo {
    public String getProperty() {
      throw new RuntimeException();
    }
  }
}

Output:

java.lang.AssertionError: 
Expecting
  AssertJTest$MyThrowingPojo@be64738
to have a property or a field named "property"
(static and synthetic fields are ignored)

	at AssertJTest.test(AssertJTest.java:9)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)

(no trace of the RuntimeException – note that the test succeeds when getProperty() returns "value")

The problem appears to be that the nice IntrospectionError built by PropertyOrFieldSupport#getSimpleValue() is discarded in org.assertj.core.internal.Objects#assertHasFieldOrProperty().

In my specific case, the object under test was implemented by proxy which was throwing the exception, which makes it really confusing.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions