Skip to content

Comments

Replace Junit4 assertions by AssertJ ones#333

Merged
VanRoy merged 1 commit intoassertj:mainfrom
jmaniquet:task/migrate-tests-suite-assertions-to-assertj
Feb 21, 2026
Merged

Replace Junit4 assertions by AssertJ ones#333
VanRoy merged 1 commit intoassertj:mainfrom
jmaniquet:task/migrate-tests-suite-assertions-to-assertj

Conversation

@jmaniquet
Copy link
Contributor

This solves #332

I used OpenRewrite as suggested by @scordio ; though not the recipe suggested as it was a major refactoring of the code base beyond the scope of the ticket.
Altering the code on a large scale was quite fast with that tool, I did liked it, great tool.

To find the right recipes, I did spent some time analyzing what needed changes though.
There was only three types of old assertions that I found :

  • A few org.junit.Assert.assertTrue
  • A few org.junit.Assert.assertFalse
  • Lots of org.junit.Assert.fail

The first two were those I saw when working on #329 and that's what initiated my suggestion.
There were very few; I just happened to find by accident the only two classes that had them (Order_Equals_Test / Values_AreEqual_UUID_And_String_Test).
All the other files changed were using org.junit.Assert.fail in try/fail/catch blocks

I spent some more time with OpenRewrite to see if I could change the try/fail/catch blocks to one of the Throwable-related AssertJ assertions.
I did find a recipe : https://docs.openrewrite.org/recipes/java/testing/assertj/junittryfailtoassertthatthrownby
However it seems to only work with empty catch blocks
And beside that, there are multiple ways in AssertJ to assert Throwables. This may not have been the one you would have chosen.
So I left try/fail/catch as is for now.

I also played with the recipe to replace Assertions.assertThat by the static import.
It works, but i realized two things :

  • This seems to be the main usage in the code base - so this may be intended
  • They were some leftovers : I think on the classes were both assertThat (from core and from db) have to coexist - so my second guess is the non-static import comes from that constraint

I left that as is for now too; though maybe it can now be solved thanks to #329

As for OpenRewrite : I did not commit the config I used. Once run, I see it as dead code in the POM.
That's just me however, and I can alter the POM with the OpenRewrite config I used if you wish.

As for the recipes as I used : I did not find any that migrated Junit4 assertions to AssertJ ones.
There was however :

  • One that migrate Junit4 assertions to Junit Jupiter ones
  • And several recipes to migrate Junit Jupiter ones to AssertJ ones

For reference, I ended up with this :

      <plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>6.29.0</version>
        <configuration>
          <exportDatatables>true</exportDatatables>
          <activeRecipes>
            <recipe>org.openrewrite.java.testing.junit5.AssertToAssertions</recipe>
            <recipe>org.openrewrite.java.testing.assertj.JUnitAssertTrueToAssertThat</recipe>
            <recipe>org.openrewrite.java.testing.assertj.JUnitAssertFalseToAssertThat</recipe>
            <recipe>org.openrewrite.java.testing.assertj.JUnitFailToAssertJFail</recipe>
          </activeRecipes>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.openrewrite.recipe</groupId>
            <artifactId>rewrite-testing-frameworks</artifactId>
            <version>3.27.0</version>
          </dependency>
        </dependencies>
      </plugin>

If there is any interest, I'm willing to do more test-related refactorings; I have done this kind of work before.

There was only three found :

- A few assertTrue
- A few assertFalse
- Lots of org.junit.Assert.fail
@VanRoy
Copy link
Member

VanRoy commented Feb 21, 2026

@jmaniquet Thank you for that and for this very detailed description!
That cover exactly the purpose of the ticket.

Regarding a more deep refactoring of test we have to evaluate if it worth.
To be honest if you can continue to spend time on assertj-db contribution I prefer we invest more a new feature / bugfix for now and come back to the tests part later.

Again thanks a lot for your help.

@VanRoy VanRoy merged commit 6c7c9b9 into assertj:main Feb 21, 2026
1 check passed
@jmaniquet jmaniquet deleted the task/migrate-tests-suite-assertions-to-assertj branch February 21, 2026 11:52
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.

2 participants