Return to site

๐Ÿคฉ ASSERTJ SOFT ASSERTIONS: CLEANER, SAFER TESTING โœ…

September 27, 2025

When writing tests, failing fast isnโ€™t always the best. Sometimes, you want to collect all assertion failures in one go. Thatโ€™s where AssertJ SoftAssertions come in ๐Ÿ’ก

๐Ÿ”ธ WHAT IS ASSERTJ? ๐Ÿค”

- AssertJ is a fluent assertion library for Java, making your tests:

- More readable (natural language style)

- More expressive (rich API, better error messages)

- More maintainable (chainable, fluent methods)

๐Ÿ”ธ CODE EXAMPLE ๐Ÿ’ป

class UserTest {

@Test

void testUserProperties() {

User user = new User("Alice", 25, "Paris");

SoftAssertions softly = new SoftAssertions();

softly.assertThat(user.getName()).isEqualTo("Bob");

softly.assertThat(user.getAge()).isGreaterThan(30);

softly.assertThat(user.getCity()).isEqualTo("London");

softly.assertAll(); // Collects and reports all failures ๐Ÿšจ

}

}

Instead of failing on the first mismatch, AssertJ reports all assertion errors together ๐Ÿ“

๐Ÿ”ธ ADVANTAGES ๐Ÿš€

- Detect multiple issues in one test run

- Provide comprehensive feedback to developers

- Reduce test re-runs ๐Ÿ”

- Keep test code concise & elegant

๐Ÿ”ธ TAKEAWAYS ๐Ÿ“Œ

- Use AssertJ to write fluent, readable, and powerful assertions

- Use SoftAssertions when you want to gather multiple failures at once

- Cleaner tests = faster debugging = happier developers ๐Ÿ˜Ž

#Java #Testing #AssertJ #UnitTesting #CleanCode #TDD