Skip to content

[POC] Migrate JUnit asserts to AssertJ - impl #2307

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Pankraz76
Copy link
Contributor

@Pankraz76 Pankraz76 commented May 9, 2025

[POC] Migrate JUnit asserts to AssertJ - impl

tryout:

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.JUnitToAssertj -Drewrite.exportDatatables=true
image image

@Pankraz76 Pankraz76 changed the title Migrate JUnit asserts to AssertJ Migrate JUnit asserts to AssertJ May 9, 2025
@Pankraz76
Copy link
Contributor Author

Pankraz76 commented May 9, 2025

bug:

[ERROR]   ReflectionValueExtractorTest.mappedIndexed:227 
expected: "a-value"
 but was: null

@Pankraz76
Copy link
Contributor Author

build success

image

@Pankraz76 Pankraz76 marked this pull request as ready for review May 9, 2025 15:50
@Pankraz76 Pankraz76 changed the title Migrate JUnit asserts to AssertJ Migrate JUnit asserts to AssertJ - POC May 9, 2025
@Pankraz76 Pankraz76 changed the title Migrate JUnit asserts to AssertJ - POC Migrate JUnit asserts to AssertJ - POC: impl/ May 9, 2025
@Pankraz76
Copy link
Contributor Author

Pankraz76 commented May 9, 2025

bug:

missing dependency check: assertj-core

      <dependency>
          <groupId>org.assertj</groupId>
          <artifactId>assertj-core</artifactId>
          <scope>test</scope>
      </dependency>
------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.14.0:testCompile (default-testCompile) on project maven-api-plugin: Compilation failure: Compilation failure: 
[ERROR] /Users/vincent.potucek/IdeaProjects/maven/api/maven-api-plugin/src/test/java/org/apache/maven/api/plugin/descriptor/another/ExtendedPluginDescriptorTest.java:[24,35] package org.assertj.core.api does not exist
[ERROR] /Users/vincent.potucek/IdeaProjects/maven/api/maven-api-plugin/src/test/java/org/apache/maven/api/plugin/descriptor/another/ExtendedPluginDescriptorTest.java:[24,1] static import only from classes and interfaces
[ERROR] /Users/vincent.potucek/IdeaProjects/maven/api/maven-api-plugin/src/test/java/org/apache/maven/api/plugin/descriptor/another/ExtendedPluginDescriptorTest.java:[74,9] cannot find symbol
[ERROR]   symbol:   method assertThat(java.lang.String)
[ERROR]   location: class org.apache.maven.api.plugin.descriptor.another.ExtendedPluginDescriptorTest
[ERROR] /Users/vincent.potucek/IdeaProjects/maven/api/maven-api-plugin/src/test/java/org/apache/maven/api/plugin/descriptor/another/ExtendedPluginDescriptorTest.java:[75,9] cannot find symbol
[ERROR]   symbol:   method assertThat(java.lang.String)
[ERROR]   location: class org.apache.maven.api.plugin.descriptor.another.ExtendedPluginDescriptorTest
[ERROR] -> [Help 1]

@Pankraz76
Copy link
Contributor Author

kindly request some feedback

Copy link
Contributor

@gnodet gnodet left a comment

Choose a reason for hiding this comment

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

The PR contains unrelated commis.

@Pankraz76
Copy link
Contributor Author

Pankraz76 commented May 11, 2025

The PR contains unrelated commis.

yes will squash.

This anyway just demo.

I would suggest to go with one single source of truth and not pay double the cost of carry and raising questions.

Where was the question about migrating to AssertJ risen and discussed already? Please provide me a link, I might have overseen it in my mails.

Need to pitch and sync with community. Might be obvious that we benefit from strict diet, but of course all need to be heard, as SSOT is just an illusion and individual.

@Bukama @olamy

@Pankraz76
Copy link
Contributor Author

Pankraz76 commented May 11, 2025

Illustration of New Code Already Being Out of Sync (Considered Outdated)

JUnit vs AssertJ Comparison

Medium article reference

In summary:

  • JUnit: Testing framework for defining, executing, and reporting tests
  • AssertJ: Library that enhances assertion readability and expressiveness

Personal Experience

In my last project using JUnit5, we still maintained a modern assertThat structure.

StackOverflow reference about the assertThat method.

Proposal: Standardizing Test Style

Key points:

  1. Adopt a single test style using the generic assertThat pattern
  2. Both JUnit and AssertJ share this API design pattern
  3. The actual framework implementation becomes an abstraction layer detail

Current problems:

  • Mixing assertTrue and assertThat creates inconsistency
  • No common base increases setup time
  • Requires recognizing different patterns for the same purpose

Goal:

  • Reduce complexity from 3 patterns to 1
  • Framework choice becomes a separate discussion

@Pankraz76 Pankraz76 changed the title Migrate JUnit asserts to AssertJ - POC: impl/ [POC] Migrate JUnit asserts to AssertJ - impl May 11, 2025
@Pankraz76 Pankraz76 force-pushed the fix-write-fix-JUnitToAssertj branch from e3d5e17 to 432c7a3 Compare May 11, 2025 09:18
@Pankraz76 Pankraz76 requested a review from gnodet May 11, 2025 09:20
assertNotNull(v);
assertEquals("", v.toString());
assertThat(v).isNotNull();
assertThat(v.toString()).isEqualTo("");
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any isEmpty() assertion ?

Copy link

Choose a reason for hiding this comment

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

Is there any isEmpty() assertion ?

If not, you can create custom assertions for AssertJ very easly. I do this quite a lot at work for custom objects/classes

Copy link
Contributor Author

@Pankraz76 Pankraz76 May 11, 2025

Choose a reason for hiding this comment

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

might be a bug, as failed to choose: https://docs.openrewrite.org/recipes/java/testing/assertj/simplifyassertjassertion

@timtebeek

Or simply need to chain.

Copy link
Contributor Author

@Pankraz76 Pankraz76 May 11, 2025

Choose a reason for hiding this comment

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

Any change not standardized is weakened.

Simplest way would be JUnit Jupiter best practices and allow both.

or (continuous) migrate JUnit asserts to AssertJ like seen here.

Both will cure generic assertTrue statements elevate into assertThat.
Which lib is actually an random impl. detail.

assertNotEquals(v2, v1, "expected " + v2 + " != " + v1);
assertThat(Integer.signum(v1.compareTo(v2))).as("expected " + v1 + " > " + v2).isEqualTo(1);
assertThat(Integer.signum(v2.compareTo(v1))).as("expected " + v2 + " < " + v1).isEqualTo(-1);
assertThat(v2).as("expected " + v1 + " != " + v2).isNotEqualTo(v1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Aren't those messages automatically generated by assertJ in case of failures ?

Copy link
Contributor

@gnodet gnodet left a comment

Choose a reason for hiding this comment

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

I think there's still room for improvements.
It would be nice if the details of the commit include a command used to refactor.

@Pankraz76
Copy link
Contributor Author

It would be nice if the details of the commit include a command used to refactor.

you mean this?

https://docs.openrewrite.org/recipes/java/testing/assertj/junittoassertj#usage

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.JUnitToAssertj -Drewrite.exportDatatables=true

@Pankraz76
Copy link
Contributor Author

I think there's still room for improvements.

I would argue against this change now, realising and remembering that JUnit 5 looks almost 1o1 the same like AssertJ.

Both following and featuring assertThat prefix.

We could apply the API changes avoiding generic fluff assertTrue but use JUnit as its mandatory, which AssertJ might be not anymore.

@Pankraz76
Copy link
Contributor Author

JUnit 5 is equal, therefore superior to AssertJ.

only need to apply: JUnit Jupiter best practices

image

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