hasAllNullFieldsOrProperties check that the actual object has only null fields or properties.
All fields and properties are inspected but fields with primitive types always will have some value.
class Book {
String title;
int pages;
}
@Test
void testHasAllNullFieldsOrProperties() {
Book book = new Book();
assertThat(book).hasAllNullFieldsOrProperties();
}
test will fail because of field pages is not null.
Exclude by default all field with primitive types from assertion.
I want to have a try on the issue, thanks.
@nach-o-man I guess he meant primitive types should be ignored and not be considered when using hasAllNullFieldsOrProperties. Otherwise, if a class has primitive fields, the test will always fail. I'm not sure.
Fair point, I understood this wrong when I read it for the first time. But I do think it is not right add such functionality.
This exactly falling test case can show design/logic flaws and can be avoided with the use of hasNoNullFieldsOrPropertiesExcept
I used hasNoNullFieldsOrPropertiesExcept at current time with list of field which should be excluded.
Primitive types always have value, so. we always must exclude it.
As my proposition in issue its can be excluded / skipped by default.
@slawekjaranowski I will have a try and make a pull request to skip primitive types by default. I may add a new method.
@wuqihan837 before submitting a PR, give the assertj team a bit of time to think on how to address the issue.
My first comment would be that the current assertion does its job as primitive types are never null, changing it would be a breaking change so we might choose a different option, maybe overloading hasAllNullFieldsOrProperties(boolean excludePrimitiveTypes) but no decision has been reached yet.
The definition of field / property does not exclude primitive types, so I don't see any issue with the current behavior of hasAllNullFieldsOrProperties.
To simplify the primitive types use case, I vote for adding hasAllNullFieldsOrProperties(boolean excludePrimitiveTypes) or also hasAllNullFieldsOrPropertiesExceptPrimitiveTypes().
I vote for hasAllNullFieldsOrPropertiesExceptPrimitiveTypes(), it is a long name but it is explicit whereas hasAllNullFieldsOrProperties(true) is not really self explanatory.
Most helpful comment
@wuqihan837 before submitting a PR, give the assertj team a bit of time to think on how to address the issue.
My first comment would be that the current assertion does its job as primitive types are never null, changing it would be a breaking change so we might choose a different option, maybe overloading
hasAllNullFieldsOrProperties(boolean excludePrimitiveTypes)but no decision has been reached yet.