Skip to content

Latest commit

 

History

History
64 lines (43 loc) · 3.27 KB

File metadata and controls

64 lines (43 loc) · 3.27 KB

Better Assertions

A quick demo application to show various assertion (anti-)patterns, and how to improve those with OpenRewrite recipes.

Bad examples

A bit of a rant on bad testing practices seen over the years.

  1. No tests at all.
  2. Code coverage tests
  3. Plain assert
  4. AssertEquals list size
  5. AssertTrue list contains
  6. Failure message argument order
  7. Try catch and fail
  8. AssertThrows catch early

Dated examples

Beyond just bad practices, there are also just outdated practices.

  1. JUnit 3
  2. JUnit 4
  3. Hamcrest
  4. JUnit Jupiter
  5. AssertJ
  6. Outdated Java in tests

Better examples

A few examples of my favorite features of AssertJ.

  1. Chained assertions
  2. Custom AssertionProvider
  3. Soft assertions
  4. Satisfies

Automatic conversion

A quick command to improve the above tests by adopting AssertJ.

mvn rewrite:run -Drewrite.activeRecipes=com.github.timtebeek.BetterAssertions

A workshop with related material on automated upgrades can be found at:

  1. https://openrewrite.github.io/break-your-testing-habits/docs/intro

AssertJ's own examples

AssertJ themselves also have a helpful, if incomplete example repository.

  1. https://github.com/assertj/assertj-examples/
  2. https://github.com/assertj/assertj-examples/commit/72c339f0f72fd4a6e903c25af2d3f093702f3e4c

MockMvc examples

Finally, MockMvc tests can now use AssertJ instead of Hamcrest for assertions.

  1. MockMvc test with Hamcrest
  2. MockMvc test with AssertJ
  3. MockMvc test with bridge
  4. Migrate MockMvc to AssertJ