@Test public void should_print_line_numbers_of_failed_assertions() { BDDSoftAssertions softly = new BDDSoftAssertions(); try { softly.then(1).isLessThan(0) .isLessThan(1); softly.assertAll(); fail("Should not reach here"); } catch (SoftAssertionError e) { assertThat(e.getMessage()).contains(format("1) %n" + "Expecting:%n" + " <1>%n" + "to be less than:%n" + " <0> %n" + "at BDDSoftAssertionsLineNumberTest.should_print_line_numbers_of_failed_assertions(BDDSoftAssertionsLineNumberTest.java:34)%n" + "2) %n" + "Expecting:%n" + " <1>%n" + "to be less than:%n" + " <1> %n" + "at BDDSoftAssertionsLineNumberTest.should_print_line_numbers_of_failed_assertions(BDDSoftAssertionsLineNumberTest.java:35)")); } }
@Test public void should_print_line_numbers_of_failed_assertions() { try { AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions(); softly.assertThat(1).isLessThan(0) .isLessThan(1); softly.close(); fail("Should not reach here"); } catch (SoftAssertionError e) { assertThat(e.getMessage()).contains(format("1) %n" + "Expecting:%n" + " <1>%n" + "to be less than:%n" + " <0> %n" + "at AutoClosableSoftAssertionsLineNumberTest.should_print_line_numbers_of_failed_assertions(AutoClosableSoftAssertionsLineNumberTest.java:34)%n" + "2) %n" + "Expecting:%n" + " <1>%n" + "to be less than:%n" + " <1> %n" + "at AutoClosableSoftAssertionsLineNumberTest.should_print_line_numbers_of_failed_assertions(AutoClosableSoftAssertionsLineNumberTest.java:35)")); } }
@Test public void should_print_line_numbers_of_failed_assertions() { SoftAssertions softly = new SoftAssertions(); try { softly.assertThat(1).isLessThan(0) .isLessThan(1); softly.assertAll(); fail("Should not reach here"); } catch (SoftAssertionError e) { assertThat(e.getMessage()).contains(format("1) %n" + "Expecting:%n" + " <1>%n" + "to be less than:%n" + " <0> %n" + "at SoftAssertionsLineNumberTest.should_print_line_numbers_of_failed_assertions(SoftAssertionsLineNumberTest.java:37)%n" + "2) %n" + "Expecting:%n" + " <1>%n" + "to be less than:%n" + " <1> %n" + "at SoftAssertionsLineNumberTest.should_print_line_numbers_of_failed_assertions(SoftAssertionsLineNumberTest.java:38)")); } }
/** * Verifies that no proxied assertion methods have failed. * * @throws SoftAssertionError if any proxied assertion objects threw */ public void assertAll() { List<Throwable> errors = collector.errors(); if (!errors.isEmpty()) { throw new SoftAssertionError(extractProperty("message", String.class).from(errors)); } }
void checkSheetContents(Workbook workbook, int sheetIndex, List<String>... rows) { try (AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) { checkSheetContents(softly, workbook, sheetIndex, rows); } catch (SoftAssertionError sae) { System.out.println("===========\nACTUAL\n==========="); printSheet(workbook.getSheetAt(sheetIndex)); System.out.println("===========\nEXPECTED\n==========="); printSheet(asList(rows)); throw sae; } }
@Test public void should_print_line_numbers_of_failed_assertions_even_if_it_came_from_nested_calls() { SoftAssertions softly = new SoftAssertions(); try { softly.assertThat(Optional.empty()).contains("Foo"); // nested proxied call to isNotNull softly.assertThat((Predicate<String>) null).accepts("a", "b", "c"); Predicate<String> lowercasePredicate = s -> s.equals(s.toLowerCase()); // check line number softly.assertThat(lowercasePredicate).accepts("a", "b", "C"); softly.assertAll(); fail("Should not reach here"); } catch (SoftAssertionError e) { assertThat(e.getMessage()).contains(format("1) %n" + "Expecting Optional to contain:%n" + " <\"Foo\">%n" + "but was empty.%n" + "at SoftAssertionsLineNumberTest.should_print_line_numbers_of_failed_assertions_even_if_it_came_from_nested_calls(SoftAssertionsLineNumberTest.java:61)%n" + "2) %n" + "Expecting actual not to be null%n" + "at SoftAssertionsLineNumberTest.should_print_line_numbers_of_failed_assertions_even_if_it_came_from_nested_calls(SoftAssertionsLineNumberTest.java:63)%n" + "3) %n" + "Expecting all elements of:%n" + " <[\"a\", \"b\", \"C\"]>%n" + "to match given predicate but this element did not:%n" + " <\"C\">%n" + "at SoftAssertionsLineNumberTest.should_print_line_numbers_of_failed_assertions_even_if_it_came_from_nested_calls(SoftAssertionsLineNumberTest.java:66)")); } }