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

Introduction to AssertJ

Introduction to AssertJ

by Andy Chen at TWJUG 20201021 https://twjug.kktix.cc/events/twjug202010

LINE Developers Taiwan

October 21, 2020
Tweet

More Decks by LINE Developers Taiwan

Other Decks in Technology

Transcript

  1. 2YHUYLHZ • )OXHQWH[SUHVVLRQ • 6LPSOLILFDWLRQ • 5HDGDELOLW\ • %HWWHUHUURUGHVFULSWLRQ •

    (DVLHUKDQGOLQJRIRIWHQXVHGW\SHV ([FHSWLRQ2SWLRQDO)XWXUHŏ
  2. )OXHQW([SUHVVLRQ // JUnit 4 assertEquals(expected, actual); assertEquals("Brown", brown.getName()); assertNotEquals("Sally", brown.getName());

    assertTrue(brown.isBear()); assertFalse(brown.isRabbit()); // AssertJ assertThat(actual) .isEqualTo(expected); assertThat(brown.getName()) .isEqualTo("Brown"); assertThat(brown.getName()) .isNotEqualTo("Sally"); assertThat(brown.isBear()) .isTrue(); assertThat(brown.isRabbit()) .isFalse();
  3. )OXHQW([SUHVVLRQ 5HGXFHJHWWHU WHPSRUDU\YDULDEOH // JUnit 4 assertEquals(3, friends.size()); assertEquals("Sally", friends.get(2).getName());

    assertFalse(friends.get(2).isBear()); // assert for other fields? // assert for friends[0], friends[1]? // AssertJ assertThat(friends) .hasSize(3) .extracting(Person::getName, Person::isBear) .contains(tuple("Sally", false), atIndex(2));
  4. )OXHQW([SUHVVLRQ 5HGXFHJHWWHU WHPSRUDU\YDULDEOH FRQWŇG // extracting by function assertThat(brown) .extracting(Person::getName,

    person -> person.getFriend().getName()) .containsExactly("Brown", "Cony"); // or by field name (support nested field) assertThat(brown) .extracting("name", ”friend.name") .containsExactly("Brown", "Cony"); // or against a collection assertThat(List.of(brown, sally)) .extracting("name", ”friend.name") .containsExactly(tuple("Brown", "Cony"), tuple("Sally", null)); class Person { String name; Person friend; }
  5. )OXHQW([SUHVVLRQ &ROOHFWLRQDVVHUWLQJPHPEHU assertThat(List.of(brown, sally, cony, brown)) // contains given item

    .contains(brown) // contains given item at specific index .contains(cony, atIndex(2)) // contains only these items, can repeats .containsOnly(sally, cony, brown) // contains exactly these items, in given order .containsExactly(brown, sally, cony, brown) // contains exactly these items, but in any order .containsExactlyInAnyOrder(brown, brown, cony, sally) // and more...
  6. )OXHQW([SUHVVLRQ &ROOHFWLRQDVVHUWLQJPHPEHU assertThat(Map.of("Brown", brown, "Sally", sally)) // contains given keys

    .containsKeys("Sally", "Brown") // contains given values .containsValues(sally, brown) // contains given key-value pairs .contains(entry("Brown", brown)) // or extracting values from entry .extractingFromEntries(Entry::getKey, e -> e.getValue().isBear()) .contains(tuple("Brown", true));
  7. %HWWHU(UURU'HVFULSWLRQ 0RUHH[SUHVVLYH // JUnit 4 assertThat(friends, hasSize(3)); assertThat(friends.get(0).getName(), is("Brown")); assertThat(friends.get(0).isBear(),

    is(true)); assertThat(friends.get(1).getName(), is("Cony")); assertThat(friends.get(1).isBear(), is(false)); assertThat(friends.get(2).getName(), is("Sally")); assertThat(friends.get(2).isBear(), is(false)); Expected: is "Cony" but: was "Sally" // AssertJ assertThat(friends) .hasSize(3) .extracting("name", "age", "bear") .containsExactly(tuple("Brown", 18, true), tuple("Cony", 12, false), tuple("Sally", 6, false)); Actual and expected have the same elements but not in the same order, at index 1 actual element was: <("Sally", 6, false)> whereas expected element was: <("Cony", 12, false)>
  8. %HWWHU(UURU'HVFULSWLRQ 6RIWDVVHUWLRQVVKRZDOOHUURULQVWHDGRIWKHILUVWRQH // JUnit 4 assertThat(brown.getName(), is("Sally")); // would not

    execute rest of the test assertThat(sally.getName(), is("Sally")); assertThat(cony.getName(), is("Sally")); Expected: is "Sally" but: was "Brown" // AssertJ final var softly = new SoftAssertions(); softly.assertThat(brown.getName()).isEqualTo("Sally"); softly.assertThat(sally.getName()).isEqualTo("Sally"); softly.assertThat(cony.getName()).isEqualTo("Sally"); softly.assertAll(); Multiple Failures (2 failures) -- failure 1 -- Expecting: <"Brown"> to be equal to: <"Sally"> but was not. at AssertJTest.softAssertions(AssertJTest.java:104) -- failure 2 -- Expecting: <"Cony"> to be equal to: <"Sally"> but was not. at AssertJTest.softAssertions(AssertJTest.java:106)
  9. ([FHSWLRQDVVHUWLRQV // JUnit 4 @Test(expected = RuntimeException.class) public void expectException()

    { // which one actually throws exception? createMockData(); methodWouldThrow(); } // need to use rule for asserting properties @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void expectExceptionWithMessage() { // which one actually throws exception? createMockData(); thrown.expect(RuntimeException.class); thrown.expectMessage(is("Err...")); methodWouldThrow(); } // AssertJ @Test void expectExceptionWithMessage() { // clear to see which is expected to throw createMockData(); assertThatThrownBy(() -> methodWouldThrow()) .isInstanceOf(RuntimeException.class) .hasMessage("Err..."); }
  10. 2SWLRQDODVVHUWLRQV // AssertJ assertThat(Optional.empty()) .isEmpty() assertThat(optional) .isPresent() .hasValue(brown) // or

    assert against the object .get() .extracting(Person::getName) .isEqualTo("Brown"); // JUnit 4 assertTrue(Optional.empty().isEmpty()); assertFalse(optional.isEmpty()); assertThat(optional.get(), is(brown)); assertThat(optional.get().getName(), is("Brown"));
  11. &RPSOHWDEOH)XWXUH DVVHUWLRQV // AssertJ assertThat(completedFuture) .isCompleted() .isCompletedWithValue(value) // or assert

    against object .succeedsWithin(100, MILLISECONDS) .isEqualTo(value); assertThat(failedFuture) .isCompletedExceptionally() .hasFailed() .hasFailedWithThrowableThat() // same as asserting exception .isInstanceOf(RuntimeException.class); assertThat(canceledFuture) .isCompletedExceptionally() .isCancelled(); // JUnit 4 assertTrue( completedFuture.isDone() && !completedFuture.isCompletedExceptionally()); assertThat(completedFuture.join(), is(value)); assertTrue( failedFuture.isCompletedExceptionally() && !failedFuture.isCancelled()); // this would throw CompletionException, // complex to assert underlying exception failedFuture.join();