Migrating from Hamcrest to AssertJ with OpenRewrite and Moderne

Tim te Beek
|
August 23, 2023
Tech stack liquidity

Key Takeaways

When it comes to the technology we use, times are often changing. What’s common or fashionable one moment might be stale and outdated the next. Libraries and frameworks come and go…or linger. This is especially true in large code bases and organizations. As you step through the code, you will often see a reflection of what was popular over time, as well as attempts to migrate to the next new thing.

Is Hamcrest doing all that it could for you?

One library that was popular some time ago is Hamcrest. Hamcrest provides various matchers that you can use in unit tests to assert that the actual result you received from an operation matches an expected outcome.

Here’s what a test typically looks like in Hamcrest, as taken from their documentation:

Nowadays, Hamcrest still works, and you’ll come across it often, especially in more established projects. However, Hamcrest has not seen a new release since 2019, and there’s a growing number of open issues and pull requests on the project.

AssertJ: Rich assertions for the win

When it comes to doing expressive assertion these days, there is more activity on AssertJ, despite these libraries each existing for a similar timespan.

Here’s what a test typically looks like in AssertJ, as converted from the above Hamcrest example:

Notice how AssertJ only requires a single static import, with an API that’s highly discoverable. There’s a rich number of assertions you can express using AssertJ, that translate into easy-to-read messages when assertions fail. These qualities might tempt you to migrate, but that can seem daunting when you have a large body of tests built up over time.

Migrating from Hamcrest to AssertJ

At Moderne, we recently invested time into automating the migration from Hamcrest to AssertJ. You can see some of these individual migration recipes in our documentation. These recipes convert a large body of common Hamcrest expressions into AssertJ assertions. You can run these recipes individually, but you’ll get better results when you run them as part of our AssertJ best practices.

For Maven, you can migrate with a single command (Gradle projects can be migrated in a similar way).

When run, you will see the Hamcrest example above converted to the AssertJ example below. Along the way, we will also adopt dedicated AssertJ assertions, for more expressive assertion error messages, as seen in this example pull request for Spring Security applied through Moderne.

Tech stack liquidity—adapt and accelerate

Together, Moderne and OpenRewrite allow you to quickly and easily migrate from one library to another. By combining individual OpenRewrite recipes and running them at scale through Moderne, you can migrate your entire organization from Hamcrest (or JUnit) to AssertJ in minutes. And it’s not just this migration, but a whole ecosystem of accurate code changes to choose from.

At Moderne, we enable organizations to quickly move from one thing to another, no matter where they are coming from in their migration—this can mean migrating to new versions or migrating to new tech. That makes it easier to reason across projects and keep projects maintainable over time.

Colorful, semi-transparent Moderne symbol breaking apart like a puzzleColorful, semi-transparent Moderne symbol breaking apart like a puzzleColorful, semi-transparent Moderne symbol breaking apart like a puzzle

Back to Blog

Colorful, semi-transparent Moderne symbol breaking apart like a puzzleColorful, semi-transparent Moderne symbol breaking apart like a puzzleColorful, semi-transparent Moderne symbol breaking apart like a puzzle

Back to Engineering Blog