Skip to content

Conversation

@quaff
Copy link
Contributor

@quaff quaff commented Oct 17, 2024

  1. Improve null safety
  2. Replace lambda with method reference

1. Improve null safety
2. Replace lambda with method reference
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 17, 2024
@mhalbritter mhalbritter added type: task A general task and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 17, 2024
@mhalbritter mhalbritter added this to the 3.2.x milestone Oct 17, 2024
@mhalbritter mhalbritter changed the title Polishing tests Polish tests Oct 17, 2024
void getWhenHasValidatedBeanBindsWithBeanAnnotation() throws Throwable {
get(ValidatedBeanConfiguration.class, "validatedBean", (propertiesBean) -> {
Validated validated = propertiesBean.asBindTarget().getAnnotation(Validated.class);
assertThat(validated).isNotNull();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is an improvement. It makes the test more verbose and I think the benefit is negligible. Should validated be null, the NPE that would occur on the next line when calling validated.value() will make that clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes IDEA and code analysis tools happy, is it worthy?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think so.

void getWhenHasValidatedFactoryMethodBindsWithFactoryMethodAnnotation() throws Throwable {
get(ValidatedMethodConfiguration.class, "annotatedBean", (propertiesBean) -> {
Validated validated = propertiesBean.asBindTarget().getAnnotation(Validated.class);
assertThat(validated).isNotNull();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above. Please revert.

void getWhenHasValidatedBeanAndFactoryMethodBindsWithFactoryMethodAnnotation() throws Throwable {
get(ValidatedMethodAndBeanConfiguration.class, "validatedBean", (propertiesBean) -> {
Validated validated = propertiesBean.asBindTarget().getAnnotation(Validated.class);
assertThat(validated).isNotNull();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above. Please revert.

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example.class);
ConfigurationPropertiesBean bean = ConfigurationPropertiesBean.get(applicationContext,
applicationContext.getBean(Example.class), "example");
assertThat(bean).isNotNull();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above. Please revert. Additionally, getBean(Class) will never return null, throwing a NoSuchBeanDefinitionException instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConfigurationPropertiesBean.get() is @Nullable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I misread the diff. Regardless, the same reasoning as above still applies. I don't think null checks such as this in tests are beneficial.

void boundPropertiesShouldBeRecorded() {
load(NestedConfiguration.class, "name=foo", "nested.name=bar");
BoundConfigurationProperties bound = BoundConfigurationProperties.get(this.context);
assertThat(bound).isNotNull();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above. Please revert.

@wilkinsona wilkinsona modified the milestones: 3.2.x, 3.4.x Oct 18, 2024
@wilkinsona wilkinsona added the for: merge-with-amendments Needs some changes when we merge label Oct 18, 2024
@wilkinsona wilkinsona modified the milestones: 3.4.x, 3.4.0-RC1 Oct 18, 2024
wilkinsona pushed a commit that referenced this pull request Oct 18, 2024
Replace lambdas with method references

See gh-42725
@wilkinsona
Copy link
Member

Thanks, @quaff.

I've merged this without the null checks. We can consider this topic again as and when we tackle #10712, building on the Framework team's experience where they treat main and test source differently.

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

Labels

for: merge-with-amendments Needs some changes when we merge type: task A general task

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants