Best Assertj code snippet using org.assertj.core.api.BDDSoftAssertionsTest
Source:BDDSoftAssertionsTest.java
...71import static org.assertj.core.data.TolkienCharacter.Race.ELF;72import static org.assertj.core.data.TolkienCharacter.Race.HOBBIT;73import static org.assertj.core.data.TolkienCharacter.Race.MAN;74import static org.assertj.core.util.Arrays.array;75public class BDDSoftAssertionsTest extends BaseAssertionsTest {76 private BDDSoftAssertions softly;77 private CartoonCharacter homer;78 private CartoonCharacter fred;79 private CartoonCharacter lisa;80 private CartoonCharacter maggie;81 private CartoonCharacter bart;82 private Map<String, Object> iterableMap;83 private ThrowingExtractor<Name, String, Exception> throwingFirstNameExtractor;84 private ThrowingExtractor<Name, String, Exception> throwingLastNameExtractor;85 private Function<Name, String> firstNameFunction;86 private Function<Name, String> lastNameFunction;87 private Function<? super CartoonCharacter, ? extends Collection<CartoonCharacter>> childrenExtractor;88 @Test89 public void all_assertions_should_pass() {90 softly.then(1).isEqualTo(1);91 softly.then(Lists.newArrayList(1, 2)).containsOnly(1, 2);92 softly.assertAll();93 }94 @Test95 public void should_return_success_of_last_assertion() {96 softly.then(true).isFalse();97 softly.then(true).isEqualTo(true);98 Assertions.assertThat(softly.wasSuccess()).isTrue();99 }100 @Test101 public void should_return_success_of_last_assertion_with_nested_calls() {102 softly.then(true).isFalse();103 softly.then(true).isTrue();// isTrue() calls isEqualTo(true)104 Assertions.assertThat(softly.wasSuccess()).isTrue();105 }106 @Test107 public void should_return_failure_of_last_assertion() {108 softly.then(true).isTrue();109 softly.then(true).isEqualTo(false);110 Assertions.assertThat(softly.wasSuccess()).isFalse();111 }112 @Test113 public void should_return_failure_of_last_assertion_with_nested_calls() {114 softly.then(true).isTrue();115 softly.then(true).isFalse();// isFalse() calls isEqualTo(false)116 Assertions.assertThat(softly.wasSuccess()).isFalse();117 }118 @SuppressWarnings("unchecked")119 @Test120 public void should_be_able_to_catch_exceptions_thrown_by_map_assertions() {121 // GIVEN122 Map<String, String> map = Maps.mapOf(MapEntry.entry("54", "55"));123 // WHEN124 softly.then(map).contains(MapEntry.entry("1", "2")).isEmpty();125 // THEN126 List<Throwable> errors = softly.errorsCollected();127 Assertions.assertThat(errors).hasSize(2);128 Assertions.assertThat(errors.get(0)).hasMessageContaining(String.format(("Expecting:%n" + ((((" <{\"54\"=\"55\"}>%n" + "to contain:%n") + " <[MapEntry[key=\"1\", value=\"2\"]]>%n") + "but could not find:%n") + " <[MapEntry[key=\"1\", value=\"2\"]]>%n"))));129 Assertions.assertThat(errors.get(1)).hasMessageContaining(String.format("Expecting empty but was:<{\"54\"=\"55\"}>"));130 }131 @SuppressWarnings("unchecked")132 @Test133 public void should_be_able_to_catch_exceptions_thrown_by_all_proxied_methods() throws URISyntaxException {134 // GIVEN135 softly.then(BigDecimal.ZERO).isEqualTo(BigDecimal.ONE);136 softly.then(Boolean.FALSE).isTrue();137 softly.then(false).isTrue();138 softly.then(new boolean[]{ false }).isEqualTo(new boolean[]{ true });139 softly.then(new Byte(((byte) (0)))).isEqualTo(((byte) (1)));140 softly.then(((byte) (2))).inHexadecimal().isEqualTo(((byte) (3)));141 softly.then(new byte[]{ 4 }).isEqualTo(new byte[]{ 5 });142 softly.then(new Character(((char) (65)))).isEqualTo(new Character(((char) (66))));143 softly.then(((char) (67))).isEqualTo(((char) (68)));144 softly.then(new char[]{ 69 }).isEqualTo(new char[]{ 70 });145 softly.then(new StringBuilder("a")).isEqualTo(new StringBuilder("b"));146 softly.then(Object.class).isEqualTo(String.class);147 softly.then(DateUtil.parseDatetime("1999-12-31T23:59:59")).isEqualTo(DateUtil.parseDatetime("2000-01-01T00:00:01"));148 softly.then(new Double(6.0)).isEqualTo(new Double(7.0));149 softly.then(8.0).isEqualTo(9.0);150 softly.then(new double[]{ 10.0 }).isEqualTo(new double[]{ 11.0 });151 softly.then(new File("a")).overridingErrorMessage(String.format("%nExpecting:%n <File(a)>%nto be equal to:%n <File(b)>%nbut was not.")).isEqualTo(new File("b"));152 softly.then(new Float(12.0F)).isEqualTo(new Float(13.0F));153 softly.then(14.0F).isEqualTo(15.0F);154 softly.then(new float[]{ 16.0F }).isEqualTo(new float[]{ 17.0F });155 softly.then(new ByteArrayInputStream(new byte[]{ ((byte) (65)) })).hasSameContentAs(new ByteArrayInputStream(new byte[]{ ((byte) (66)) }));156 softly.then(new Integer(20)).isEqualTo(new Integer(21));157 softly.then(22).isEqualTo(23);158 softly.then(new int[]{ 24 }).isEqualTo(new int[]{ 25 });159 softly.then(((Iterable<String>) (Lists.newArrayList("26")))).isEqualTo(Lists.newArrayList("27"));160 softly.then(Lists.newArrayList("28").iterator()).isExhausted();161 softly.then(Lists.newArrayList("30")).isEqualTo(Lists.newArrayList("31"));162 softly.then(new Long(32L)).isEqualTo(new Long(33L));163 softly.then(34L).isEqualTo(35L);164 softly.then(new long[]{ 36L }).isEqualTo(new long[]{ 37L });165 softly.then(Maps.mapOf(MapEntry.entry("38", "39"))).isEqualTo(Maps.mapOf(MapEntry.entry("40", "41")));166 softly.then(new Short(((short) (42)))).isEqualTo(new Short(((short) (43))));167 softly.then(((short) (44))).isEqualTo(((short) (45)));168 softly.then(new short[]{ ((short) (46)) }).isEqualTo(new short[]{ ((short) (47)) });169 softly.then("48").isEqualTo("49");170 softly.then(new Object() {171 @Override172 public String toString() {173 return "50";174 }175 }).isEqualTo(new Object() {176 @Override177 public String toString() {178 return "51";179 }180 });181 softly.then(new Object[]{ new Object() {182 @Override183 public String toString() {184 return "52";185 }186 } }).isEqualTo(new Object[]{ new Object() {187 @Override188 public String toString() {189 return "53";190 }191 } });192 final IllegalArgumentException illegalArgumentException = new IllegalArgumentException("IllegalArgumentException message");193 softly.then(illegalArgumentException).hasMessage("NullPointerException message");194 softly.thenThrownBy(() -> {195 throw new Exception("something was wrong");196 }).hasMessage("something was good");197 softly.then(Maps.mapOf(MapEntry.entry("54", "55"))).contains(MapEntry.entry("1", "2"));198 softly.then(LocalTime.of(12, 0)).isEqualTo(LocalTime.of(13, 0));199 softly.then(OffsetTime.of(12, 0, 0, 0, ZoneOffset.UTC)).isEqualTo(OffsetTime.of(13, 0, 0, 0, ZoneOffset.UTC));200 softly.then(Optional.of("not empty")).isEqualTo("empty");201 softly.then(OptionalInt.of(0)).isEqualTo(1);202 softly.then(OptionalDouble.of(0.0)).isEqualTo(1.0);203 softly.then(OptionalLong.of(0L)).isEqualTo(1L);204 softly.then(new URI("http://assertj.org")).hasPort(8888);205 softly.then(CompletableFuture.completedFuture("done")).hasFailed();206 softly.then(((Predicate<String>) (( s) -> s.equals("something")))).accepts("something else");207 softly.then(((IntPredicate) (( s) -> s == 1))).accepts(2);208 softly.then(((LongPredicate) (( s) -> s == 1))).accepts(2);209 softly.then(((DoublePredicate) (( s) -> s == 1))).accepts(2);210 // WHEN211 MultipleFailuresError error = Assertions.catchThrowableOfType(() -> softly.assertAll(), MultipleFailuresError.class);212 // THEN213 List<String> errors = error.getFailures().stream().map(Object::toString).collect(Collectors.toList());214 Assertions.assertThat(errors).hasSize(52);215 Assertions.assertThat(errors.get(0)).contains(String.format("%nExpecting:%n <0>%nto be equal to:%n <1>%nbut was not."));216 Assertions.assertThat(errors.get(1)).contains(String.format("%nExpecting:%n <false>%nto be equal to:%n <true>%nbut was not."));217 Assertions.assertThat(errors.get(2)).contains(String.format("%nExpecting:%n <false>%nto be equal to:%n <true>%nbut was not."));218 Assertions.assertThat(errors.get(3)).contains(String.format("%nExpecting:%n <[false]>%nto be equal to:%n <[true]>%nbut was not."));219 Assertions.assertThat(errors.get(4)).contains(String.format("%nExpecting:%n <0>%nto be equal to:%n <1>%nbut was not."));220 Assertions.assertThat(errors.get(5)).contains(String.format("%nExpecting:%n <0x02>%nto be equal to:%n <0x03>%nbut was not."));221 Assertions.assertThat(errors.get(6)).contains(String.format("%nExpecting:%n <[4]>%nto be equal to:%n <[5]>%nbut was not."));222 Assertions.assertThat(errors.get(7)).contains(String.format("%nExpecting:%n <'A'>%nto be equal to:%n <'B'>%nbut was not."));223 Assertions.assertThat(errors.get(8)).contains(String.format("%nExpecting:%n <'C'>%nto be equal to:%n <'D'>%nbut was not."));224 Assertions.assertThat(errors.get(9)).contains(String.format("%nExpecting:%n <['E']>%nto be equal to:%n <['F']>%nbut was not."));225 Assertions.assertThat(errors.get(10)).contains(String.format("%nExpecting:%n <a>%nto be equal to:%n <b>%nbut was not."));226 Assertions.assertThat(errors.get(11)).contains(String.format("%nExpecting:%n <java.lang.Object>%nto be equal to:%n <java.lang.String>%nbut was not."));227 Assertions.assertThat(errors.get(12)).contains(String.format("%nExpecting:%n <1999-12-31T23:59:59.000>%nto be equal to:%n <2000-01-01T00:00:01.000>%nbut was not."));228 Assertions.assertThat(errors.get(13)).contains(String.format("%nExpecting:%n <6.0>%nto be equal to:%n <7.0>%nbut was not."));229 Assertions.assertThat(errors.get(14)).contains(String.format("%nExpecting:%n <8.0>%nto be equal to:%n <9.0>%nbut was not."));230 Assertions.assertThat(errors.get(15)).contains(String.format("%nExpecting:%n <[10.0]>%nto be equal to:%n <[11.0]>%nbut was not."));231 Assertions.assertThat(errors.get(16)).contains(String.format("%nExpecting:%n <File(a)>%nto be equal to:%n <File(b)>%nbut was not."));232 Assertions.assertThat(errors.get(17)).contains(String.format("%nExpecting:%n <12.0f>%nto be equal to:%n <13.0f>%nbut was not."));233 Assertions.assertThat(errors.get(18)).contains(String.format("%nExpecting:%n <14.0f>%nto be equal to:%n <15.0f>%nbut was not."));234 Assertions.assertThat(errors.get(19)).contains(String.format("%nExpecting:%n <[16.0f]>%nto be equal to:%n <[17.0f]>%nbut was not."));235 Assertions.assertThat(errors.get(20)).contains(String.format(("%nInputStreams do not have same content:%n%n" + (((("Changed content at line 1:%n" + "expecting:%n") + " [\"B\"]%n") + "but was:%n") + " [\"A\"]%n"))));236 Assertions.assertThat(errors.get(21)).contains(String.format("%nExpecting:%n <20>%nto be equal to:%n <21>%nbut was not."));237 Assertions.assertThat(errors.get(22)).contains(String.format("%nExpecting:%n <22>%nto be equal to:%n <23>%nbut was not."));238 Assertions.assertThat(errors.get(23)).contains(String.format("%nExpecting:%n <[24]>%nto be equal to:%n <[25]>%nbut was not."));239 Assertions.assertThat(errors.get(24)).contains(String.format("%nExpecting:%n <[\"26\"]>%nto be equal to:%n <[\"27\"]>%nbut was not."));240 Assertions.assertThat(errors.get(25)).contains(String.format("Expecting the iterator under test to be exhausted"));241 Assertions.assertThat(errors.get(26)).contains(String.format("%nExpecting:%n <[\"30\"]>%nto be equal to:%n <[\"31\"]>%nbut was not."));242 Assertions.assertThat(errors.get(27)).contains(String.format("%nExpecting:%n <32L>%nto be equal to:%n <33L>%nbut was not."));243 Assertions.assertThat(errors.get(28)).contains(String.format("%nExpecting:%n <34L>%nto be equal to:%n <35L>%nbut was not."));244 Assertions.assertThat(errors.get(29)).contains(String.format("%nExpecting:%n <[36L]>%nto be equal to:%n <[37L]>%nbut was not."));245 Assertions.assertThat(errors.get(30)).contains(String.format("%nExpecting:%n <{\"38\"=\"39\"}>%nto be equal to:%n <{\"40\"=\"41\"}>%nbut was not."));246 Assertions.assertThat(errors.get(31)).contains(String.format("%nExpecting:%n <42>%nto be equal to:%n <43>%nbut was not."));247 Assertions.assertThat(errors.get(32)).contains(String.format("%nExpecting:%n <44>%nto be equal to:%n <45>%nbut was not."));248 Assertions.assertThat(errors.get(33)).contains(String.format("%nExpecting:%n <[46]>%nto be equal to:%n <[47]>%nbut was not."));249 Assertions.assertThat(errors.get(34)).contains(String.format("%nExpecting:%n <\"48\">%nto be equal to:%n <\"49\">%nbut was not."));250 Assertions.assertThat(errors.get(35)).contains(String.format("%nExpecting:%n <50>%nto be equal to:%n <51>%nbut was not."));251 Assertions.assertThat(errors.get(36)).contains(String.format("%nExpecting:%n <[52]>%nto be equal to:%n <[53]>%nbut was not."));252 Assertions.assertThat(errors.get(37)).contains(String.format(("%nExpecting message:%n" + ((" <\"NullPointerException message\">%n" + "but was:%n") + " <\"IllegalArgumentException message\">"))));253 Assertions.assertThat(errors.get(38)).contains(String.format(("%nExpecting message:%n" + ((" <\"something was good\">%n" + "but was:%n") + " <\"something was wrong\">"))));254 Assertions.assertThat(errors.get(39)).contains(String.format(("%nExpecting:%n" + ((((" <{\"54\"=\"55\"}>%n" + "to contain:%n") + " <[MapEntry[key=\"1\", value=\"2\"]]>%n") + "but could not find:%n") + " <[MapEntry[key=\"1\", value=\"2\"]]>%n"))));255 Assertions.assertThat(errors.get(40)).contains(String.format("%nExpecting:%n <12:00>%nto be equal to:%n <13:00>%nbut was not."));256 Assertions.assertThat(errors.get(41)).contains(String.format("%nExpecting:%n <12:00Z>%nto be equal to:%n <13:00Z>%nbut was not."));257 Assertions.assertThat(errors.get(42)).contains(String.format("%nExpecting:%n <Optional[not empty]>%nto be equal to:%n <\"empty\">%nbut was not."));258 Assertions.assertThat(errors.get(43)).contains(String.format("%nExpecting:%n <OptionalInt[0]>%nto be equal to:%n <1>%nbut was not."));259 Assertions.assertThat(errors.get(44)).contains(String.format("%nExpecting:%n <OptionalDouble[0.0]>%nto be equal to:%n <1.0>%nbut was not."));260 Assertions.assertThat(errors.get(45)).contains(String.format("%nExpecting:%n <OptionalLong[0]>%nto be equal to:%n <1L>%nbut was not."));261 Assertions.assertThat(errors.get(46)).contains("Expecting port of");262 Assertions.assertThat(errors.get(47)).contains("to have failed");263 Assertions.assertThat(errors.get(48)).contains(String.format(("%nExpecting:%n <given predicate>%n" + "to accept <\"something else\"> but it did not.")));264 Assertions.assertThat(errors.get(49)).contains(String.format(("%nExpecting:%n <given predicate>%n" + "to accept <2> but it did not.")));265 Assertions.assertThat(errors.get(50)).contains(String.format(("%nExpecting:%n <given predicate>%n" + "to accept <2L> but it did not.")));266 Assertions.assertThat(errors.get(51)).contains(String.format(("%nExpecting:%n <given predicate>%n" + "to accept <2.0> but it did not.")));267 }268 @SuppressWarnings("unchecked")269 @Test270 public void should_pass_when_using_extracting_with_object() {271 // GIVEN272 Name name = Name.name("John", "Doe");273 // WHEN274 softly.then(name).extracting("first", "last").contains("John", "Doe");275 softly.then(name).extracting(Name::getFirst, Name::getLast).contains("John", "Doe");276 softly.then(name).extracting(Name::getFirst).isEqualTo("John");277 // THEN278 Assertions.assertThat(softly.errorsCollected()).isEmpty();279 }280 @Test281 public void should_pass_when_using_extracting_with_list() {282 // GIVEN283 List<Name> names = Arrays.asList(Name.name("John", "Doe"), Name.name("Jane", "Doe"));284 // WHEN285 softly.then(names).extracting("first").as("using extracting()").contains("John").contains("Jane").contains("Foo1");286 softly.then(names).extracting(Name::getFirst).as("using extracting(Extractor)").contains("John").contains("Jane").contains("Foo2");287 softly.then(names).extracting("first", String.class).as("using extracting(..., Class)").contains("John").contains("Jane").contains("Foo3");288 softly.then(names).extracting("first", "last").as("using extracting(...)").contains(Assertions.tuple("John", "Doe")).contains(Assertions.tuple("Jane", "Doe")).contains(Assertions.tuple("Foo", "4"));289 softly.then(names).extractingResultOf("getFirst", String.class).as("using extractingResultOf(method, Class)").contains("John").contains("Jane").contains("Foo5");290 softly.then(names).extractingResultOf("getFirst").as("using extractingResultOf(method)").contains("John").contains("Jane").contains("Foo6");291 // THEN292 List<Throwable> errorsCollected = softly.errorsCollected();293 Assertions.assertThat(errorsCollected).hasSize(6);294 Assertions.assertThat(errorsCollected.get(0)).hasMessageContaining("Foo1");295 Assertions.assertThat(errorsCollected.get(1)).hasMessageContaining("Foo2");296 Assertions.assertThat(errorsCollected.get(2)).hasMessageContaining("Foo3");297 Assertions.assertThat(errorsCollected.get(3)).hasMessageContaining("Foo").hasMessageContaining("4");298 Assertions.assertThat(errorsCollected.get(4)).hasMessageContaining("Foo5");299 Assertions.assertThat(errorsCollected.get(5)).hasMessageContaining("Foo6");300 }301 @Test302 public void should_pass_when_using_extracting_with_iterable() {303 Iterable<Name> names = Arrays.asList(Name.name("John", "Doe"), Name.name("Jane", "Doe"));304 try (AutoCloseableBDDSoftAssertions softly = new AutoCloseableBDDSoftAssertions()) {305 softly.then(names).extracting("first").as("using extracting()").contains("John").contains("Jane");306 softly.then(names).extracting(Name::getFirst).as("using extracting(Extractor)").contains("John").contains("Jane");307 softly.then(names).extracting("first", String.class).as("using extracting(..., Class)").contains("John").contains("Jane");308 softly.then(names).extracting("first", "last").as("using extracting(...)").contains(Assertions.tuple("John", "Doe")).contains(Assertions.tuple("Jane", "Doe"));309 softly.then(names).extractingResultOf("getFirst", String.class).as("using extractingResultOf(method, Class)").contains("John").contains("Jane");310 softly.then(names).extractingResultOf("getFirst").as("using extractingResultOf(method)").contains("John").contains("Jane");311 }312 }313 @Test314 public void should_work_when_using_extracting_with_array() {315 Name[] namesAsArray = new Name[]{ Name.name("John", "Doe"), Name.name("Jane", "Doe") };316 try (AutoCloseableBDDSoftAssertions softly = new AutoCloseableBDDSoftAssertions()) {317 softly.then(namesAsArray).extracting("first").as("using extracting()").contains("John").contains("Jane");318 softly.then(namesAsArray).extracting(Name::getFirst).as("using extracting(Extractor)").contains("John").contains("Jane");319 softly.then(namesAsArray).extracting("first", String.class).as("using extracting(..., Class)").contains("John").contains("Jane");320 softly.then(namesAsArray).extracting("first", "last").as("using extracting(...)").contains(Assertions.tuple("John", "Doe")).contains(Assertions.tuple("Jane", "Doe"));321 softly.then(namesAsArray).extractingResultOf("getFirst", String.class).as("using extractingResultOf(method, Class)").contains("John").contains("Jane");322 softly.then(namesAsArray).extractingResultOf("getFirst").as("using extractingResultOf(method)").contains("John").contains("Jane");323 }324 }325 @Test326 public void should_work_with_flat_extracting() {327 // GIVEN328 List<CartoonCharacter> characters = Arrays.asList(homer, fred);329 CartoonCharacter[] charactersAsArray = characters.toArray(new CartoonCharacter[characters.size()]);330 // WHEN331 softly.then(characters).flatExtracting(CartoonCharacter::getChildren).as("using flatExtracting on Iterable").containsAnyOf(homer, fred).hasSize(123);332 softly.then(charactersAsArray).flatExtracting(CartoonCharacter::getChildren).as("using flatExtracting on array").containsAnyOf(homer, fred).hasSize(456);333 // THEN334 List<Throwable> errorsCollected = softly.errorsCollected();335 Assertions.assertThat(errorsCollected).hasSize(4);336 Assertions.assertThat(errorsCollected.get(0)).hasMessageContaining(homer.toString());337 Assertions.assertThat(errorsCollected.get(1)).hasMessageContaining("123");338 Assertions.assertThat(errorsCollected.get(2)).hasMessageContaining(fred.toString());339 Assertions.assertThat(errorsCollected.get(3)).hasMessageContaining("456");340 }341 @Test342 public void should_collect_all_errors_when_using_extracting() {343 // GIVEN344 List<Name> names = Arrays.asList(Name.name("John", "Doe"), Name.name("Jane", "Doe"));345 // WHEN346 softly.then(names).extracting("first").overridingErrorMessage("error 1").contains("gandalf").overridingErrorMessage("error 2").contains("frodo");347 softly.then(names).extracting("last").overridingErrorMessage("error 3").isEmpty();348 // THEN349 Assertions.assertThat(softly.errorsCollected()).extracting(Throwable::getMessage).containsExactly("error 1", "error 2", "error 3");350 }351 @Test352 public void should_collect_all_errors_when_using_flat_extracting() {353 // GIVEN354 List<CartoonCharacter> characters = Arrays.asList(homer, fred);355 // WHEN356 softly.then(characters).flatExtracting(CartoonCharacter::getChildren).overridingErrorMessage("error 1").hasSize(0).overridingErrorMessage("error 2").isEmpty();357 // THEN358 Assertions.assertThat(softly.errorsCollected()).extracting(Throwable::getMessage).containsExactly("error 1", "error 2");359 }360 @Test361 public void should_collect_all_errors_when_using_filtering() {362 // GIVEN363 LinkedHashSet<CartoonCharacter> dads = Sets.newLinkedHashSet(homer, fred);364 // WHEN365 softly.then(dads).filteredOn("name", "Homer Simpson").hasSize(10).isEmpty();366 // THEN367 List<Throwable> errorsCollected = softly.errorsCollected();368 Assertions.assertThat(errorsCollected).hasSize(2);369 Assertions.assertThat(errorsCollected.get(0)).hasMessageStartingWith(String.format("%nExpected size:<10> but was:<1> in:%n<[CartoonCharacter [name=Homer Simpson]]>"));370 Assertions.assertThat(errorsCollected.get(1)).hasMessageStartingWith(String.format("%nExpecting empty but was:<[CartoonCharacter [name=Homer Simpson]]>"));371 }372 @Test373 public void should_collect_all_errors_when_using_predicate_filtering() {374 // GIVEN375 LinkedHashSet<CartoonCharacter> dads = Sets.newLinkedHashSet(homer, fred);376 // WHEN377 softly.then(dads).filteredOn(( c) -> c.getName().equals("Homer Simpson")).hasSize(10).isEmpty();378 // THEN379 List<Throwable> errorsCollected = softly.errorsCollected();380 Assertions.assertThat(errorsCollected).hasSize(2);381 Assertions.assertThat(errorsCollected.get(0)).hasMessageStartingWith(String.format("%nExpected size:<10> but was:<1> in:%n<[CartoonCharacter [name=Homer Simpson]]>"));382 Assertions.assertThat(errorsCollected.get(1)).hasMessageStartingWith(String.format("%nExpecting empty but was:<[CartoonCharacter [name=Homer Simpson]]>"));383 }384 @Test385 public void should_work_with_comparable() {386 // GIVEN387 ComparableExample example1 = new ComparableExample(0);388 ComparableExample example2 = new ComparableExample(0);389 ComparableExample example3 = new ComparableExample(123);390 // WHEN391 softly.then(example1).isEqualByComparingTo(example2);392 softly.then(example1).isEqualByComparingTo(example3);393 // THEN394 List<Throwable> errorsCollected = softly.errorsCollected();395 Assertions.assertThat(errorsCollected).hasSize(1);396 Assertions.assertThat(errorsCollected.get(0)).hasMessageContaining("123");397 }398 @Test399 public void should_work_with_stream() {400 // WHEN401 softly.then(Stream.of("a", "b", "c")).contains("a", "foo");402 softly.then(IntStream.of(1, 2, 3)).contains(2, 4, 6);403 softly.then(LongStream.of(1, 2, 3)).contains((-1L), (-2L), (-3L));404 softly.then(DoubleStream.of(1, 2, 3)).contains(10.0, 20.0, 30.0);405 // THEN406 List<Throwable> errorsCollected = softly.errorsCollected();407 Assertions.assertThat(errorsCollected).hasSize(4);408 Assertions.assertThat(errorsCollected.get(0)).hasMessageContaining("foo");409 Assertions.assertThat(errorsCollected.get(1)).hasMessageContaining("6");410 Assertions.assertThat(errorsCollected.get(2)).hasMessageContaining("-3");411 Assertions.assertThat(errorsCollected.get(3)).hasMessageContaining("30.0");412 }413 @Test414 public void should_work_with_CompletionStage() {415 // GIVEN416 CompletionStage<String> completionStage = CompletableFuture.completedFuture("done");417 // WHEN418 softly.then(completionStage).isDone();419 softly.then(completionStage).hasNotFailed();420 softly.then(completionStage).isCancelled();421 completionStage = null;422 softly.then(completionStage).isNull();423 // THEN424 Assertions.assertThat(softly.errorsCollected()).hasSize(1);425 Assertions.assertThat(softly.errorsCollected().get(0)).hasMessageContaining("cancelled");426 }427 @Test428 public void should_work_with_predicate() {429 // GIVEN430 Predicate<String> lowercasePredicate = ( s) -> s.equals(s.toLowerCase());431 // WHEN432 softly.then(lowercasePredicate).accepts("a", "b", "c");433 softly.then(lowercasePredicate).accepts("a", "b", "C");434 // THEN435 Assertions.assertThat(softly.errorsCollected()).hasSize(1);436 Assertions.assertThat(softly.errorsCollected().get(0)).hasMessageContaining("C");437 }438 @Test439 public void should_work_with_optional() {440 // GIVEN441 Optional<String> optional = Optional.of("Gandalf");442 // WHEN443 softly.then(optional).contains("Gandalf");444 // THEN445 softly.assertAll();446 }447 @Test448 public void should_work_with_optional_chained_with_map() {449 // GIVEN450 Optional<String> optional = Optional.of("Gandalf");451 // WHEN452 softly.then(optional).contains("Gandalf").map(String::length).contains(7);453 // THEN454 softly.assertAll();455 }456 @Test457 public void should_collect_all_errors_when_using_map() {458 // GIVEN459 Optional<String> optional = Optional.of("Gandalf");460 // WHEN461 softly.then(optional).contains("Sauron");462 softly.then(optional).contains("Gandalf").map(String::length).contains(1);463 // THEN464 Assertions.assertThat(softly.errorsCollected()).hasSize(2);465 }466 @Test467 public void should_collect_all_errors_when_using_flatMap() {468 // GIVEN469 Optional<String> optional = Optional.of("Gandalf");470 // WHEN471 softly.then(optional).contains("Sauron");472 softly.then(optional).flatMap(( s) -> Optional.of(s.length())).contains(1);473 // THEN474 Assertions.assertThat(softly.errorsCollected()).hasSize(2);475 }476 @Test477 public void should_propagate_AssertionError_from_nested_proxied_calls() {478 // the nested proxied call to isNotEmpty() throw an Assertion error that must be propagated to the caller.479 softly.then(Arrays.asList()).first();480 // nested proxied call to throwAssertionError when checking that is optional is present481 softly.then(Optional.empty()).contains("Foo");482 // nested proxied call to isNotNull483 softly.then(((Predicate<String>) (null))).accepts("a", "b", "c");484 // nested proxied call to isCompleted485 softly.then(new CompletableFuture<String>()).isCompletedWithValue("done");486 // it must be caught by softly.assertAll()487 Assertions.assertThat(softly.errorsCollected()).hasSize(4);488 }489 // bug #447490 public class TolkienHeroe {491 String name;492 int age;493 }494 @Test495 public void check_477_bugfix() {496 // GIVEN497 BDDSoftAssertionsTest.TolkienHeroe frodo = new BDDSoftAssertionsTest.TolkienHeroe();498 BDDSoftAssertionsTest.TolkienHeroe samnullGamgee = null;499 BDDSoftAssertionsTest.TolkienSoftAssertions softly = new BDDSoftAssertionsTest.TolkienSoftAssertions();500 // WHEN501 softly.then(frodo).hasAge(10);// Expect failure - age will be 0 due to not being initialized.502 softly.then(samnullGamgee).hasAge(11);// Expect failure - argument is null.503 // THEN504 Assertions.assertThat(errorsCollected()).hasSize(2);505 }506 public static class TolkienHeroesAssert extends AbstractAssert<BDDSoftAssertionsTest.TolkienHeroesAssert, BDDSoftAssertionsTest.TolkienHeroe> {507 public TolkienHeroesAssert(BDDSoftAssertionsTest.TolkienHeroe actual) {508 super(actual, BDDSoftAssertionsTest.TolkienHeroesAssert.class);509 }510 public static BDDSoftAssertionsTest.TolkienHeroesAssert assertThat(BDDSoftAssertionsTest.TolkienHeroe actual) {511 return new BDDSoftAssertionsTest.TolkienHeroesAssert(actual);512 }513 // 4 - a specific assertion !514 public BDDSoftAssertionsTest.TolkienHeroesAssert hasName(String name) {515 // check that actual TolkienCharacter we want to make assertions on is not null.516 isNotNull();517 // check condition518 if (!(Objects.equals(name, name))) {519 failWithMessage("Expected character's name to be <%s> but was <%s>", name, name);520 }521 // return the current assertion for method chaining522 return this;523 }524 // 4 - another specific assertion !525 public BDDSoftAssertionsTest.TolkienHeroesAssert hasAge(int age) {526 // check that actual TolkienCharacter we want to make assertions on is not null.527 isNotNull();528 // check condition529 if ((age) != age) {530 failWithMessage("Expected character's age to be <%s> but was <%s>", age, actual.age);531 }532 // return the current assertion for method chaining533 return this;534 }535 }536 public static class TolkienSoftAssertions extends SoftAssertions {537 public BDDSoftAssertionsTest.TolkienHeroesAssert then(BDDSoftAssertionsTest.TolkienHeroe actual) {538 return proxy(BDDSoftAssertionsTest.TolkienHeroesAssert.class, BDDSoftAssertionsTest.TolkienHeroe.class, actual);539 }540 }541 @Test542 public void should_return_failure_after_fail() {543 // GIVEN544 String failureMessage = "Should not reach here";545 // WHEN546 softly.fail(failureMessage);547 // THEN548 Assertions.assertThat(softly.wasSuccess()).isFalse();549 Assertions.assertThat(softly.errorsCollected()).hasSize(1);550 Assertions.assertThat(softly.errorsCollected().get(0)).hasMessageStartingWith(failureMessage);551 }552 @Test...
Source:org.assertj.core.api.BDDSoftAssertionsTest-all_assertions_should_pass.java
...23import org.assertj.core.test.Maps;24import org.assertj.core.util.Lists;25import org.junit.Before;26import org.junit.Test;27public class BDDSoftAssertionsTest {28 private BDDSoftAssertions softly;29 30 @Before31 public void setup() {32 softly = new BDDSoftAssertions();33 }34 35 @Test36 public void all_assertions_should_pass() {37 softly.then(1).isEqualTo(1);38 softly.then(Lists.newArrayList(1, 2)).containsOnly(1, 2);39 softly.assertAll();40 }41}...
BDDSoftAssertionsTest
Using AI Code Generation
1import org.assertj.core.api.BDDSoftAssertionsTest;2publc class Main {3 public static void ain(String[] args) {4 BDDSoftAssertionsTest bddsoftassertionstest = new BDDSoftAssertionsTest();5 bddsoftassertionstest.testSoftlyWithBDDSoftAssertions();6 }7}8 aassert.core.api.BDDSoftAssertionsTest.testSoftlyWithBDDSoftAssertions(BDDSoftAssertionsTest.java:26)
BDDSoftAssertionsTest
Using AI Code Generation
1import org.junit.Test;public class Main {2 public static void main(String[] args) {3 BDDSoftAssertionsTest bddsoftassertionstest = new BDDSoftAssertionsTest();4 bddsoftassertionstest.testSoftlyWithBDDSoftAssertions();5 }6}7 at org.assertj.core.api.BDDSoftAssertionsTest.testSoftlyWithBDDSoftAssertions(BDDSoftAssertionsTest.java:26)
BDDSoftAssertionsTest
Using AI Code Generation
1import org.assertj.core.api.BDDSoftAssertionsTest;2import org.assertj.core.api.BDDSoftAssertions;3import org.assertj.core.api.SoftAssertionsProvider;4public class BDDSoftAssertionsTest1 {5 public static void main(String[] args) {6 BDDSoftAssertionsTest obj = new BDDSoftAssertionsTest();7 obj.should_delegate_to_soft_assertions();8 }9}
BDDSoftAssertionsTest
Using AI Code Generation
1import org.assertj.core.api.BDDSoftAssertionsTest;2class Test {3 public static void main(String[] args) {4 BDDSoftAssertionsTest obj = new BDDSoftAssertionsTest();5 obj.softAssertion();6 }7}8at org.assertj.core.api.DSoftAssertionsTest.softAssertion(BDDSoftAssertionsTest.java:26)9import org.assertj.core.api.BDDSoftAssertionsTest;10import org.junit.Test;11public class BDDSoftAssertionsTest {12 public void testBDDSoftAssertions() {13 BDDSoftAssertions softly = new BDDSoftAssertions();14 softly.then("abc").isEqualTo("abc");15 softly.then("abc").isEqualTo("ab");16 softly.assertAll();17 }18}19 at org.junit.Assert.assertEquals(Assert.java:115)20 at org.junit.Assert.assertEquals(Assert.java:144)21 at org.assertj.core.api.BDDSoftAssertionsTest.testBDDSoftAssertions(BDDSoftAssertionsTest.java:9)
BDDSoftAssertionsTest
Using AI Code Generation
1import org.assertj.core.api.AssSoftertionsTesTest;2import org.junit.jupiter.api.t ob;3publicj lass BDDSoftAssertionsTest1 {4pub=ic void test1() {5BDDSoftAssertionsTest bddsoft nsertionstest = new BDDSoftAssertionsTest();6bddsoftassertionstest.BDDSoftAssertionsTest();7}8}9at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:162)10at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:149)11at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:92)12at org.junit.platform.console.tasks.ConsoleTestExecutor.executeTests(ConsoleTestExecutor.java:65)13at org.junit.platform.console.tasks.ConsoleTestExecutor.lambda$execute$0(ConsoleTestExecutor.java:57)14at org.junit.platform.console.tasks.CustomContextClassLoaderExecutor.invoke(CustomContextClassLoaderExecutor.java:33)15at org.junit.platform.console.tasks.ConsoleTestExecutor.execute(ConsoleTestExecutor.java:57)16at org.junit.platform.console.ConsoleLauncher.executeTests(ConsoleLauncher.java:84)17at org.junit.platform.console.ConsoleLauncher.execute(ConsoleLauncher.java:74)18at org.junit.platform.console.ConsoleLauncher.execute(ConsoleLauncher.java:47)19at org.junit.platform.console.ConsoleLauncher.main(ConeoleLauncher.java:39)w BDDSoftAssertionsTest();20 obj.should_delegate_to_soft_assertions();21at org.junit.platform.launcher.core.DefaultLauncher.disco erEngineRoot(Def ultLauncher.java:162)}~[junitplatform-launcher-1.7.1.jar:1.7.1]22at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:149) ~[junit-platform-launcher-1.7.1.jar:1.7.1]23at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:92) ~[junit-platform-launcher-1.7.1.jar:1.7.1]24at org.junit.platform.console.tasks.ConsoleTestExecutor.executeTests(ConsoleTestExecutor.java:65) ~[classes/:?]25at org.junit.platform.console.tasks.ConsoleTestExecutor.lambda$execute$0(ConsoleTestExecutor.java:57) ~[classes/:?]26at org.junit.platform.console.tasks.CustomContextClassLoaderExecutor.invoke(CustomContextClassLoaderExecutor.java:33) ~[classes
BDDSoftAssertionsTest
Using AI Code Generation
1package com.automationrhapsody.junitbdd;2import org.assertj.core.api.BDDSoftAssertionsTest;3import org.junit.Test;4public class BDDSoftAssertionsTestDemo {5 public void test() {6 BDDSoftAssertionsTest softAssertions = new BDDSoftAssertionsTest();7 softAssertions.then(true).isTrue();8 softAssertions.then(false).isTrue();9 softAssertions.then(true).isFalse();10 softAssertions.then(false).isFalse();11 }12}13The BDDSoftAssertionsTestDemo class is a JUnit test class, so it has a test() method. The test() method calls the then() method of the BDDSoftAssertionsTest class to create an object of the BDDSoftAssertionsTest.Then class. The BDDSoft14}
BDDSoftAssertionsTest
Using AI Code Generation
1import org.assertj.core.api.BDDSoftAssertionsTest;2class Test {3 public static void main(String[] args) {4 BDDSoftAssertionsTest obj = new BDDSoftAssertionsTest();5 obj.softAssertion();6 }7}8at org.assertj.core.api.BDDSoftAssertionsTest.softAssertion(BDDSoftAssertionsTest.java:26)
BDDSoftAssertionsTest
Using AI Code Generation
1import org.assertj.core.api.BDDSoftAssertionsTest;2import org.assertj.core.api.BDDSoftAssertionsTest;3public class BDDSoftAssertionsTest {4 public static void main(String[] args) {5 BDDSoftAssertionsTest bddsoftassertionstest = new BDDSoftAssertionsTest();6 bddsoftassertionstest.run();7 }8}9 BDDSoftAssertionsTest bddsoftassertionstest = new BDDSoftAssertionsTest();
BDDSoftAssertionsTest
Using AI Code Generation
1import org.assertj.core.api.BDDSoftAssertionsTest;2import org.junit.jupiter.api.Test;3public class BDDSoftAssertionsTest1 {4public void test1() {5BDDSoftAssertionsTest bddsoftassertionstest = new BDDSoftAssertionsTest();6bddsoftassertionstest.BDDSoftAssertionsTest();7}8}9at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:162)10at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:149)11at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:92)12at org.junit.platform.console.tasks.ConsoleTestExecutor.executeTests(ConsoleTestExecutor.java:65)13at org.junit.platform.console.tasks.ConsoleTestExecutor.lambda$execute$0(ConsoleTestExecutor.java:57)14at org.junit.platform.console.tasks.CustomContextClassLoaderExecutor.invoke(CustomContextClassLoaderExecutor.java:33)15at org.junit.platform.console.tasks.ConsoleTestExecutor.execute(ConsoleTestExecutor.java:57)16at org.junit.platform.console.ConsoleLauncher.executeTests(ConsoleLauncher.java:84)17at org.junit.platform.console.ConsoleLauncher.execute(ConsoleLauncher.java:74)18at org.junit.platform.console.ConsoleLauncher.execute(ConsoleLauncher.java:47)19at org.junit.platform.console.ConsoleLauncher.main(ConsoleLauncher.java:39)20at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:162) ~[junit-platform-launcher-1.7.1.jar:1.7.1]21at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:149) ~[junit-platform-launcher-1.7.1.jar:1.7.1]22at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:92) ~[junit-platform-launcher-1.7.1.jar:1.7.1]23at org.junit.platform.console.tasks.ConsoleTestExecutor.executeTests(ConsoleTestExecutor.java:65) ~[classes/:?]24at org.junit.platform.console.tasks.ConsoleTestExecutor.lambda$execute$0(ConsoleTestExecutor.java:57) ~[classes/:?]25at org.junit.platform.console.tasks.CustomContextClassLoaderExecutor.invoke(CustomContextClassLoaderExecutor.java:33) ~[classes
BDDSoftAssertionsTest
Using AI Code Generation
1import org.assertj.core.api.BDDSoftAssertionsTest;2public class Test{3 public static void main(String args[]){4 BDDSoftAssertionsTest test = new BDDSoftAssertionsTest();5 }6}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!