Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AssertJ: Hidden Gems of Java Developer Testing

Ted M. Young
June 22, 2023
73

AssertJ: Hidden Gems of Java Developer Testing

Ted M. Young

June 22, 2023
Tweet

Transcript

  1. Ted M. Young https://ted.dev AssertJ Hidden Gem of Testing Ted

    M. Young Java Trainer, Coach, & Live Coder [email protected] https://Ted.dev/about
  2. Ted M. Young https://ted.dev AssertJ Poll: Raise Hands if using…

    •isEqualByComparingTo() •contains() • containsExactly() • containsExactlyInAnyOrderElementsOf() •extracting() •usingRecursiveComparison() • RecursiveComparisonConfiguration
  3. Ted M. Young https://ted.dev AssertJ is HUGE I won't cover

    (can't cover) everything, because…
  4. Ted M. Young https://ted.dev Our Agenda • Basics: comparing individual

    values • Improving output with Description • Collections and Streams • Exceptions • Raise level of abstraction • Condition • Custom assertion classes • Tools and other tips
  5. Ted M. Young https://ted.dev JUnit vs. AssertJ // JUnit assertEquals(inventoryProduct.quantityAvailable(),

    10); // AssertJ assertThat(inventoryProduct.quantityAvailable()).isEqualTo(10);
  6. Ted M. Young https://ted.dev JUnit vs. AssertJ // JUnit assertEquals(inventoryProduct.quantityAvailable(),

    10); assertEquals(10, inventoryProduct.quantityAvailable()); // AssertJ assertThat(inventoryProduct.quantityAvailable()).isEqualTo(10);
  7. Ted M. Young https://ted.dev Horizontal scrolling is even slower assertThat(rod).as("not

    == ").isNotSameAs(roderick); assertThat(rod.getName()).as("rod.name is Rod").isEqualTo("Rod"); assertThat(rod.getAge()).as("rod.age is 31").isEqualTo(31); assertThat(roderick.getName()).as("roderick.name is Roderick").isEqualTo("Roderick"); assertThat(roderick.getAge()).as("roderick.age was inherited").isEqualTo(rod.getAge());
  8. Ted M. Young https://ted.dev …to lots of simple… assertThat(rod) .as("not

    == ") .isNotSameAs(roderick); assertThat(rod.getName()) .as("rod.name is Rod") .isEqualTo("Rod"); assertThat(rod.getAge()) .as("rod.age is 31") .isEqualTo(31); assertThat(roderick.getName()) .as("roderick.name is Roderick") .isEqualTo("Roderick"); assertThat(roderick.getAge()) "Stacked" Layout
  9. Ted M. Young https://ted.dev …to the complex assertThat(productInventory) .extracting( InventoryProduct::quantityAvailable,

    InventoryProduct::price) .usingRecursiveFieldByFieldElementComparator(forBigDecimalTuple) .containsExactly( tuple(10, BigDecimal.valueOf(24.95)), tuple(1, BigDecimal.valueOf(12.00))); "Stacked" Layout
  10. Ted M. Young https://ted.dev Magic of extracting() // SimpleBook("title", "author",

    publishedYear) SimpleBook book = new SimpleBook("Mastering AssertJ", "JitterTed", 2022); assertThat(book.publishedYear()) .isEqualTo(2023); assertThat(book) .extracting("publishedYear") .isEqualTo(2023); AssertionFailedError: expected: 2023 but was: 2022 AssertionFailedError: [Extracted: publishedYear] expected: 2023 but was: 2022
  11. Ted M. Young https://ted.dev Refactor-friendly extracting() // SimpleBook("title", "author", publishedYear)

    SimpleBook book = new SimpleBook("Mastering AssertJ", "JitterTed", 2022); assertThat(book) .extracting(SimpleBook::publishedYear) .isEqualTo(2023); AssertionFailedError: expected: 2023 but was: 2022
  12. Ted M. Young https://ted.dev Not Compile-time Type-safe // SimpleBook("title", "author",

    publishedYear) SimpleBook book = new SimpleBook("Mastering AssertJ", "JitterTed", 2022); assertThat(book) .extracting(SimpleBook::publishedYear) .isEqualTo("2023"); AssertionFailedError: expected: "2022" but was: 2022
  13. Ted M. Young https://ted.dev Multiple extracting() // SimpleBook("title", "author", publishedYear)

    SimpleBook book = new SimpleBook("Mastering AssertJ", "JitterTed", 2023); assertThat(book) .extracting(SimpleBook::author, SimpleBook::publishedYear) .containsExactly("JitterTed", 2023);
  14. Ted M. Young https://ted.dev How about returns()? // SimpleBook("title", "author",

    publishedYear) SimpleBook book = new SimpleBook("Mastering AssertJ", "JitterTed", 2023); assertThat(book) .returns("2023", SimpleBook::publishedYear);
  15. Ted M. Young https://ted.dev Syntactic sugar from() doesn't help //

    SimpleBook("title", "author", publishedYear) SimpleBook book = new SimpleBook("Mastering AssertJ", "JitterTed", 2023); assertThat(book) .returns("2023", from(SimpleBook::publishedYear));
  16. Ted M. Young https://ted.dev ContainsExactly() @Test void parseWithNoQuotesParsesCorrectly() throws Exception

    { String row = "123,456,789"; List<String> values = Book.parseQuotedCsv(row); assertThat(values) .containsExactly("123", "456", "789"); }
  17. Ted M. Young https://ted.dev Predicate with Description assertThat(catalog.allBooks()) .allMatch(book ->

    book.yearPublished() >= 1996, "published since 1996") Expecting all elements of: [Book {title='Design Patterns', authors=…yearPublished=1994}, Book {title='Refactoring, 2nd Edition', authors=…yearPublished=2018}] to match 'published since 1996' predicate but this element did not: Book {title='Design Patterns', authors=…yearPublished=1994} Predicate description
  18. Ted M. Young https://ted.dev Predicate vs. Condition Expecting all elements

    of: [Book {title='Design Patterns', authors=…yearPublished=1994}, Book {title='Refactoring, 2nd Edition', authors=…yearPublished=2018}] to match 'published since 1996' predicate but this element did not: Book {title='Design Patterns', authors=…yearPublished=1994} Expecting elements: [Book {title='Design Patterns', authors=…yearPublished=1994}] of [Book {title='Design Patterns', authors=…yearPublished=1994}, Book {title='Refactoring, 2nd Edition', authors=…yearPublished=2018}] to be published in the 21st century Predicate Condition
  19. Ted M. Young https://ted.dev Blackjack Game Standard Assert @Test void

    playerStandsDealerCardsFaceUp() { Deck deck = StubDeckBuilder.playerCountOf(1) .addPlayerWithRanks(Rank.TEN, Rank.JACK) .buildWithDealerDoesNotDrawCards(); Game game = GameFactory.createOnePlayerGamePlaceBetsInitialDeal(deck); game.playerStands(); assertThat(game.dealerHand().cards()) .allMatch(card -> !card.isFaceDown()); }
  20. Ted M. Young https://ted.dev With Predicate @Test void playerStandsDealerCardsFaceUp() {

    Deck deck = StubDeckBuilder.playerCountOf(1) .addPlayerWithRanks(Rank.TEN, Rank.JACK) .buildWithDealerDoesNotDrawCards(); Game game = GameFactory.createOnePlayerGamePlaceBetsInitialDeal(deck); game.playerStands(); Predicate<Card> faceUpCardPredicate = card -> !card.isFaceDown(); assertThat(game.dealerHand().cards()) .allMatch(faceUpCardPredicate); }
  21. Ted M. Young https://ted.dev Encapsulate Assertion @Test void playerStandsDealerAllCardsFaceUp_predicate() {

    Deck deck = StubDeckBuilder.playerCountOf(1) .addPlayerWithRanks(Rank.TEN, Rank.JACK) .buildWithDealerDoesNotDrawCards(); Game game = GameFactory.createOnePlayerGamePlaceBetsInitialDeal(deck); game.playerStands(); assertAllDealerCardsFaceUp(game); } static void assertAllDealerCardsFaceUp(Game game) { assertThat(game.dealerHand().cards()) .allMatch(card -> !card.isFaceDown()); }
  22. Ted M. Young https://ted.dev Blackjack Game Custom Asserts @Test void

    playerStandsDealerCardsFaceUp() { Deck deck = StubDeckBuilder.playerCountOf(1) .addPlayerWithRanks(Rank.TEN, Rank.JACK) .buildWithDealerDoesNotDrawCards(); Game game = GameFactory.createOnePlayerGamePlaceBetsInitialDeal(deck); game.playerStands(); assertThat(game) .dealerHand() .allCardsFaceUp(); }
  23. Ted M. Young https://ted.dev Plugin helps to convert to AssertJ

    Concise AssertJ Optimizing Nitpicker (Cajon)
  24. Ted M. Young https://ted.dev AssertJ (So) Many Ways to Make

    Test Code & Results More Readable Ted M. Young Java Trainer, Coach, & Live Coder Get in touch: [email protected] Twitter: @JitterTed Twitch: https://JitterTed.LIVE YouTube: https://JitterTed.TV Web: https://ted.dev
  25. Ted M. Young https://ted.dev Thank You Have a Great Day!

    Ted M. Young Java Trainer, Coach, & Live Coder Get in touch: [email protected] Twitter: @JitterTed Twitch: https://JitterTed.LIVE YouTube: https://JitterTed.TV Web: https://ted.dev