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

Apply AssertJ best practices #1031

Merged
merged 1 commit into from
Dec 20, 2024
Merged

Conversation

timtebeek
Copy link
Contributor

What problem does this pull request solve?

Apply AssertJ best practices, such that you get more expressive failure messages.

What alternatives did you consider?

Not making these changes.

Please provide any additional information below

Ran via

mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-testing-frameworks:RELEASE -Drewrite.activeRecipes=org.openrewrite.java.testing.assertj.Assertj -Drewrite.exportDatatables=true

As per https://docs.openrewrite.org/recipes/java/testing/assertj/assertj-best-practices

Copy link
Contributor Author

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Pointed out some of the changes and why; the neat thing here is that you can also have these pointed out to you by a bot on new PRs: https://www.moderne.ai/blog/stop-breaking-ci-annotate-prs-with-openrewrite-recipe-fixes-as-quality-gate

@@ -18,8 +18,7 @@ void copyHappyPath() {
Object original = instantiate(SimpleRecord.class);
Object copy = copyOf(original);

assertThat(copy).isNotSameAs(original);
assertThat(copy).isEqualTo(original);
assertThat(copy).isNotSameAs(original).isEqualTo(original);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can chain assertions on the same element; I think that looks best with each on a new line, but spotless disagrees.

Copy link
Owner

Choose a reason for hiding this comment

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

I agree it would look better that way. It's hard to find a good formatter configuration that works well in all situations...

Copy link
Contributor

Choose a reason for hiding this comment

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

Trailing // to the rescue 😄

Suggested change
assertThat(copy).isNotSameAs(original).isEqualTo(original);
assertThat(copy) //
.isNotSameAs(original)
.isEqualTo(original);

Copy link
Owner

Choose a reason for hiding this comment

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

Haha, that's not a bad workaround, I'll keep it in mind 😄

@@ -241,7 +241,7 @@ void succeed_whenCallingForPackageRecursivelyOnAPackageContainingFailingClasses_
void succeed_whenReportingOnSeveralCorrectClasses() {
List<EqualsVerifierReport> reports = EqualsVerifier.forClasses(A.class, B.class, C.class).report();

assertThat(reports.size()).isEqualTo(3);
assertThat(reports).hasSize(3);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now when this fails you'll get a message saying "a collection was expected with three elements, but it was a collection with x elements, containing elem1, elem2, ....". Much more helpful than expected 3 but was 5.

@@ -53,7 +53,7 @@ void allValuesReturnToOriginalState_whenEqualsVerifierIsFinishedWithException()
}
catch (AssertionError e) {
// Make sure EV fails on a check that actually mutates fields.
assertThat(e.getMessage().contains("Mutability")).isTrue();
assertThat(e.getMessage()).contains("Mutability");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The assertion failure here would go from expected true was false to expected a string containing Mutability, but was ....

@@ -59,7 +59,7 @@ void onlySubClassFields() {
@Test
void noFields() {
FieldIterable iterable = FieldIterable.of(NoFields.class);
assertThat(iterable.iterator().hasNext()).isFalse();
assertThat(iterable.iterator()).isExhausted();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again; we're being more expressive about what we expect, such that you get better failure messages.

@jqno
Copy link
Owner

jqno commented Dec 20, 2024

Thank you! I was already doing some of this stuff manually, I didn't even think to look for a recipe like. Only proves my point that I haven't even scratched the surface of either OpenRewrite or AssertJ 😄

@jqno
Copy link
Owner

jqno commented Dec 20, 2024

I'll take a look at that PR bot later, that looks very promising!

@jqno jqno merged commit 77ebac8 into jqno:main Dec 20, 2024
@timtebeek timtebeek deleted the assertj-best-practices branch December 20, 2024 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants