Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use idiomatic AssertJ assertions #38702

Closed
wants to merge 1 commit into from
Closed

Conversation

quaff
Copy link
Contributor

@quaff quaff commented Dec 7, 2023

  1. Replace assertThat(x.isEmpty()).isTrue() with assertThat(x).isEmpty() Search for : assertThat((.+).isEmpty()).isTrue() Replace with : assertThat($1).isEmpty()
    Search for : assertThat((.+).isEmpty()).isFalse() Replace with : assertThat($1).isNotEmpty()

  2. Replace assertThat(x.iterator().next()) with assertThat(x).element(0) Search for : assertThat((.+).iterator().next()) Replace with : assertThat($1).element(0)

  3. Replace assertThat(x.get(i)). with assertThat(x).element(i). Search for : assertThat((.+).get((\d+))).
    Replace with : assertThat($1).element($2).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 7, 2023
@wilkinsona
Copy link
Member

Thanks, @quaff.

Search for : assertThat((.+).isEmpty()).isTrue() Replace with : assertThat($1).isEmpty()
Search for : assertThat((.+).isEmpty()).isFalse() Replace with : assertThat($1).isNotEmpty()

I like this pair of changes. Thanks.

Search for : assertThat((.+).iterator().next()) Replace with : assertThat($1).element(0)

I'm neutral on this one.

Search for : assertThat((.+).get((\d+))). Replace with : assertThat($1).element($2).

I don't like this change. It's more verbose and I don't think it makes the code easier to read.

On balance, I think my preference would be to just keep the first type of change and revert the other two. Let's see what the rest of the team thinks.

@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label Dec 7, 2023
@quaff
Copy link
Contributor Author

quaff commented Dec 7, 2023

It's more verbose and I don't think it makes the code easier to read.

It will report more meaningful error not just for readable consideration.

@wilkinsona
Copy link
Member

Deliberately breaking one of the assertions, here's the error before the proposed change:

java.lang.AssertionError: 
Expecting actual:
  org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfigurationTests$CustomMeterObservationHandler@3f07b12c
to be an instance of:
  io.micrometer.core.instrument.observation.DefaultMeterObservationHandler
but was instance of:
  org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfigurationTests.CustomMeterObservationHandler
	at org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfigurationTests.lambda$17(ObservationAutoConfigurationTests.java:252)

And here's the error afterwards:

java.lang.AssertionError: [List element at index 0] 
Expecting actual:
  org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfigurationTests$CustomMeterObservationHandler@3f07b12c
to be an instance of:
  io.micrometer.core.instrument.observation.DefaultMeterObservationHandler
but was instance of:
  org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfigurationTests.CustomMeterObservationHandler
	at org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfigurationTests.lambda$17(ObservationAutoConfigurationTests.java:252)

For me, the addition of [List element at index 0] doesn't make the error any more meaningful.

@quaff
Copy link
Contributor Author

quaff commented Dec 7, 2023

Deliberately breaking one of the assertions, here's the error before the proposed change:

java.lang.AssertionError: 
Expecting actual:
  org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfigurationTests$CustomMeterObservationHandler@3f07b12c
to be an instance of:
  io.micrometer.core.instrument.observation.DefaultMeterObservationHandler
but was instance of:
  org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfigurationTests.CustomMeterObservationHandler
	at org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfigurationTests.lambda$17(ObservationAutoConfigurationTests.java:252)

And here's the error afterwards:

java.lang.AssertionError: [List element at index 0] 
Expecting actual:
  org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfigurationTests$CustomMeterObservationHandler@3f07b12c
to be an instance of:
  io.micrometer.core.instrument.observation.DefaultMeterObservationHandler
but was instance of:
  org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfigurationTests.CustomMeterObservationHandler
	at org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfigurationTests.lambda$17(ObservationAutoConfigurationTests.java:252)

For me, the addition of [List element at index 0] doesn't make the error any more meaningful.

It's indeed more, let's wait for feedbacks from others.

@scottfrederick
Copy link
Contributor

I like 1 and 2. I do think that assertThat(foo).element(0) (or assertThat(foo).get(0)) is better than assertThat(foo.iterator().next()).

I don't like 3 either. .get(0) is more obvious than element(0).

@philwebb
Copy link
Member

philwebb commented Dec 7, 2023

I like 1 but I don't think 2 or 3 give enough value given the number of files they change.

Replace assertThat(x.isEmpty()).isTrue() with assertThat(x).isEmpty()

Search for : assertThat\((.+).isEmpty\(\)\).isTrue\(\)
Replace with : assertThat($1).isEmpty()

Search for : assertThat\((.+).isEmpty\(\)\).isFalse\(\)
Replace with : assertThat($1).isNotEmpty()
@quaff
Copy link
Contributor Author

quaff commented Dec 8, 2023

OK, I've updated the commit.

@philwebb philwebb added type: task A general task and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Dec 11, 2023
@philwebb philwebb added this to the 3.1.x milestone Dec 11, 2023
@mhalbritter mhalbritter self-assigned this Dec 12, 2023
@mhalbritter
Copy link
Contributor

Thank you!

@mhalbritter mhalbritter modified the milestones: 3.1.x, 3.1.7 Dec 12, 2023
mhalbritter pushed a commit that referenced this pull request Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: task A general task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants