Best Assertj code snippet using org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter
Source:SoftAssertionsPerfTest.java
...58 */59@Disabled60class SoftAssertionsPerfTest {61 private SoftAssertions softly;62 private CartoonCharacter homer;63 private CartoonCharacter fred;64 private CartoonCharacter lisa;65 private CartoonCharacter maggie;66 private CartoonCharacter bart;67 private Map<String, Object> iterableMap;68 private static long start;69 @BeforeAll70 static void beforeAll() {71 setRemoveAssertJRelatedElementsFromStackTrace(false);72 start = System.currentTimeMillis();73 }74 @AfterAll75 static void afterAll() {76 long duration = System.currentTimeMillis() - start;77 System.out.println("SoftAssertionsTest execution time (ms): " + duration);78 }79 @BeforeEach80 void setup() {81 softly = new SoftAssertions();82 bart = new CartoonCharacter("Bart Simpson");83 lisa = new CartoonCharacter("Lisa Simpson");84 maggie = new CartoonCharacter("Maggie Simpson");85 homer = new CartoonCharacter("Homer Simpson");86 homer.getChildren().add(bart);87 homer.getChildren().add(lisa);88 homer.getChildren().add(maggie);89 CartoonCharacter pebbles = new CartoonCharacter("Pebbles Flintstone");90 fred = new CartoonCharacter("Fred Flintstone");91 fred.getChildren().add(pebbles);92 List<String> names = asList("Dave", "Jeff");93 LinkedHashSet<String> jobs = newLinkedHashSet("Plumber", "Builder");94 Iterable<String> cities = asList("Dover", "Boston", "Paris");95 int[] ranks = { 1, 2, 3 };96 iterableMap = new LinkedHashMap<>();97 iterableMap.put("name", names);98 iterableMap.put("job", jobs);99 iterableMap.put("city", cities);100 iterableMap.put("rank", ranks);101 }102 @Test103 void all_assertions_should_pass() {104 softly.assertThat(1).isEqualTo(1);105 softly.assertThat(Lists.newArrayList(1, 2)).containsOnly(1, 2);106 softly.assertAll();107 }108 @Test109 void all_assertions_should_pass2() {110 softly.assertThat(1).isEqualTo(1);111 softly.assertThat(Lists.newArrayList(1, 2)).containsOnly(1, 2);112 softly.assertAll();113 }114 @Test115 void all_assertions_should_pass3() {116 softly.assertThat(1).isEqualTo(1);117 softly.assertThat(Lists.newArrayList(1, 2)).containsOnly(1, 2);118 softly.assertAll();119 }120 @Test121 void all_assertions_should_pass4() {122 softly.assertThat(1).isEqualTo(1);123 softly.assertThat(Lists.newArrayList(1, 2)).containsOnly(1, 2);124 softly.assertAll();125 }126 @Test127 void should_return_success_of_last_assertion() {128 softly.assertThat(true).isFalse();129 softly.assertThat(true).isEqualTo(true);130 assertThat(softly.wasSuccess()).isTrue();131 }132 @Test133 void should_return_success_of_last_assertion_with_nested_calls() {134 softly.assertThat(true).isFalse();135 softly.assertThat(true).isTrue(); // isTrue() calls isEqualTo(true)136 assertThat(softly.wasSuccess()).isTrue();137 }138 @Test139 void should_return_failure_of_last_assertion() {140 softly.assertThat(true).isTrue();141 softly.assertThat(true).isEqualTo(false);142 assertThat(softly.wasSuccess()).isFalse();143 }144 @Test145 void should_return_failure_of_last_assertion_with_nested_calls() {146 softly.assertThat(true).isTrue();147 softly.assertThat(true).isFalse(); // isFalse() calls isEqualTo(false)148 assertThat(softly.wasSuccess()).isFalse();149 }150 @Test151 void should_be_able_to_catch_exceptions_thrown_by_map_assertions() {152 // GIVEN153 Map<String, String> map = mapOf(entry("54", "55"));154 // WHEN155 softly.assertThat(map).contains(entry("1", "2")).isEmpty();156 // THEN157 List<Throwable> errors = softly.errorsCollected();158 assertThat(errors).hasSize(2);159 }160 @Test161 void should_be_able_to_catch_exceptions_thrown_by_all_proxied_methods() {162 // perform a bunch of soft assertions163 softly.assertThat(BigDecimal.ZERO).isEqualTo(BigDecimal.ONE);164 softly.assertThat(Boolean.FALSE).isTrue();165 softly.assertThat(false).isTrue();166 softly.assertThat(new boolean[] { false }).isEqualTo(new boolean[] { true });167 softly.assertThat(new Byte((byte) 0)).isEqualTo((byte) 1);168 softly.assertThat((byte) 2).inHexadecimal().isEqualTo((byte) 3);169 softly.assertThat(new byte[] { 4 }).isEqualTo(new byte[] { 5 });170 softly.assertThat(new Character((char) 65)).isEqualTo(new Character((char) 66));171 softly.assertThat((char) 67).isEqualTo((char) 68);172 softly.assertThat(new char[] { 69 }).isEqualTo(new char[] { 70 });173 softly.assertThat(new StringBuilder("a")).isEqualTo(new StringBuilder("b"));174 softly.assertThat(Object.class).isEqualTo(String.class);175 softly.assertThat(parseDatetime("1999-12-31T23:59:59")).isEqualTo(parseDatetime("2000-01-01T00:00:01"));176 softly.assertThat(new Double(6.0d)).isEqualTo(new Double(7.0d));177 softly.assertThat(8.0d).isEqualTo(9.0d);178 softly.assertThat(new double[] { 10.0d }).isEqualTo(new double[] { 11.0d });179 softly.assertThat(new File("a"))180 .overridingErrorMessage(format("%nexpected: File(a)%n but was: File(b)"))181 .isEqualTo(new File("b"));182 softly.assertThat(new Float(12f)).isEqualTo(new Float(13f));183 softly.assertThat(14f).isEqualTo(15f);184 softly.assertThat(new float[] { 16f }).isEqualTo(new float[] { 17f });185 softly.assertThat(new ByteArrayInputStream(new byte[] { (byte) 65 }))186 .hasSameContentAs(new ByteArrayInputStream(new byte[] { (byte) 66 }));187 softly.assertThat(new Integer(20)).isEqualTo(new Integer(21));188 softly.assertThat(22).isEqualTo(23);189 softly.assertThat(new int[] { 24 }).isEqualTo(new int[] { 25 });190 softly.assertThat((Iterable<String>) Lists.newArrayList("26")).isEqualTo(Lists.newArrayList("27"));191 softly.assertThat(Lists.newArrayList("28").iterator()).hasNext();192 softly.assertThat(Lists.newArrayList("30")).isEqualTo(Lists.newArrayList("31"));193 softly.assertThat(new Long(32L)).isEqualTo(new Long(33L));194 softly.assertThat(34L).isEqualTo(35L);195 softly.assertThat(new long[] { 36L }).isEqualTo(new long[] { 37L });196 softly.assertThat(mapOf(entry("38", "39"))).isEqualTo(mapOf(entry("40", "41")));197 softly.assertThat(new Short((short) 42)).isEqualTo(new Short((short) 43));198 softly.assertThat((short) 44).isEqualTo((short) 45);199 softly.assertThat(new short[] { (short) 46 }).isEqualTo(new short[] { (short) 47 });200 softly.assertThat("48").isEqualTo("49");201 softly.assertThat(new Object() {202 @Override203 public String toString() {204 return "50";205 }206 }).isEqualTo(new Object() {207 @Override208 public String toString() {209 return "51";210 }211 });212 softly.assertThat(new Object[] { new Object() {213 @Override214 public String toString() {215 return "52";216 }217 } }).isEqualTo(new Object[] { new Object() {218 @Override219 public String toString() {220 return "53";221 }222 } });223 final IllegalArgumentException illegalArgumentException = new IllegalArgumentException("IllegalArgumentException message");224 softly.assertThat(illegalArgumentException).hasMessage("NullPointerException message");225 softly.assertThatThrownBy(() -> {226 throw new Exception("something was wrong");227 }).hasMessage("something was good");228 softly.assertThat(mapOf(entry("54", "55"))).contains(entry("1", "2"));229 softly.assertThat(LocalTime.of(12, 0)).isEqualTo(LocalTime.of(13, 0));230 softly.assertThat(OffsetTime.of(12, 0, 0, 0, ZoneOffset.UTC))231 .isEqualTo(OffsetTime.of(13, 0, 0, 0, ZoneOffset.UTC));232 softly.assertThat(Optional.of("not empty")).isEqualTo("empty");233 softly.assertThat(OptionalInt.of(0)).isEqualTo(1);234 softly.assertThat(OptionalDouble.of(0.0)).isEqualTo(1.0);235 softly.assertThat(OptionalLong.of(0L)).isEqualTo(1L);236 softly.assertThat(URI.create("http://assertj.org")).hasPort(8888);237 softly.assertThat(CompletableFuture.completedFuture("done")).isCompletedExceptionally();238 softly.assertThat((Predicate<String>) s -> s.equals("something")).accepts("something else");239 softly.assertThat((IntPredicate) s -> s == 1).accepts(2);240 softly.assertThat((LongPredicate) s -> s == 1).accepts(2);241 softly.assertThat((DoublePredicate) s -> s == 1).accepts(2);242 // assert everything, but catch the error since it is a perf test243 catchThrowable(() -> softly.assertAll());244 }245 @SafeVarargs246 private static <K, V> LinkedHashMap<K, V> mapOf(MapEntry<K, V>... entries) {247 LinkedHashMap<K, V> map = new LinkedHashMap<>();248 for (Map.Entry<K, V> entry : entries) {249 map.put(entry.getKey(), entry.getValue());250 }251 return map;252 }253 private static class CartoonCharacter {254 private final String name;255 private final List<CartoonCharacter> children = new ArrayList<>();256 private CartoonCharacter(String name) {257 this.name = name;258 }259 private String getName() {260 return name;261 }262 private List<CartoonCharacter> getChildren() {263 return children;264 }265 private List<CartoonCharacter> getChildrenWithException() throws Exception {266 if (children.isEmpty()) {267 throw new Exception("Should have children");268 }269 return children;270 }271 private CartoonCharacter[] getChildrenArray() {272 return children.toArray(new CartoonCharacter[0]);273 }274 private void addChildren(CartoonCharacter... kids) {275 children.addAll(asList(kids));276 }277 @Override278 public String toString() {279 return "CartoonCharacter [name=" + name + "]";280 }281 }282}...
CartoonCharacter
Using AI Code Generation
1@BenchmarkMode(Mode.AverageTime)2@OutputTimeUnit(TimeUnit.MILLISECONDS)3@State(Scope.Benchmark)4public class SoftAssertionsPerfTest {5 private static final int NB_ITERATIONS = 1_000_000;6 private static final int NB_ASSERTIONS = 10;7 private static List<CartoonCharacter> characters = new ArrayList<>();8 @Setup(Level.Trial)9 public static void setup() {10 characters = cartoonCharacter();11 }12 public void softAssertionsWithNoError() {13 SoftAssertions softly = new SoftAssertions();14 for (int i = 0; i < NB_ASSERTIONS; i++) {15 CartoonCharacter character = characters.get(i);16 softly.assertThat(character.getName()).isEqualTo(character.getName());17 softly.assertThat(character.getAge()).isEqualTo(character.getAge());18 }19 softly.assertAll();20 }21 public void softAssertionsWithError() {22 SoftAssertions softly = new SoftAssertions();23 for (int i = 0; i < NB_ASSERTIONS; i++) {24 CartoonCharacter character = characters.get(i);25 softly.assertThat(character.getName()).isEqualTo(character.getName());26 softly.assertThat(character.getAge()).isEqualTo(character.getAge() + 1);27 }28 softly.assertAll();29 }30 public void softAssertionsWithErrorAndFailFast() {31 SoftAssertions softly = new SoftAssertions();32 for (int i = 0; i < NB_ASSERTIONS; i++) {33 CartoonCharacter character = characters.get(i);34 softly.assertThat(character.getName()).isEqualTo(character.getName());35 softly.assertThat(character.getAge()).isEqualTo(character.getAge() + 1);36 }37 softly.assertAll();38 }39 private static List<CartoonCharacter> cartoonCharacter() {40 List<CartoonCharacter> characters = new ArrayList<>();41 for (int i = 0; i < NB_ITERATIONS; i++) {42 characters.add(new CartoonCharacter("Character" + i, i));43 }44 return characters;45 }46 private static class CartoonCharacter {47 private final String name;48 private final int age;49 public CartoonCharacter(String name, int age) {50 this.name = name;51 this.age = age;52 }53 public String getName() {54 return name;55 }56 public int getAge() {
CartoonCharacter
Using AI Code Generation
1public class SoftAssertionsPerfTest {2 private static final int ITERATION_COUNT = 100000;3 private static final int WARM_UP_ITERATION_COUNT = 10000;4 private static final int WARM_UP_ITERATION_COUNT_FOR_ASSERTJ = 1000;5 private static final int ITERATION_COUNT_FOR_ASSERTJ = 100000;6 private static final String[] CHARACTERS = new String[] {"Homer", "Marge", "Bart", "Lisa", "Maggie"};7 private static final CartoonCharacter[] CARTOON_CHARACTERS = new CartoonCharacter[] {8 new CartoonCharacter("Homer", "Simpson", 40),9 new CartoonCharacter("Marge", "Simpson", 38),10 new CartoonCharacter("Bart", "Simpson", 10),11 new CartoonCharacter("Lisa", "Simpson", 8),12 new CartoonCharacter("Maggie", "Simpson", 1)13 };14 private static final CartoonCharacter[] CARTOON_CHARACTERS_WITH_NULL = new CartoonCharacter[] {15 new CartoonCharacter("Homer", "Simpson", 40),16 new CartoonCharacter("Marge", "Simpson", 38),17 new CartoonCharacter("Bart", "Simpson", 10),18 new CartoonCharacter("Maggie", "Simpson", 1)19 };20 private static final CartoonCharacter[] CARTOON_CHARACTERS_WITH_NULL_AND_EMPTY = new CartoonCharacter[] {21 new CartoonCharacter("Homer", "Simpson", 40),22 new CartoonCharacter("Marge", "Simpson", 38),23 new CartoonCharacter("Bart", "Simpson", 10),24 new CartoonCharacter("Maggie", "Simpson", 1),25 new CartoonCharacter("", "", 0)26 };27 public static void main(String[] args) throws Exception {28 System.out.println("Warm up for JUnit assertions");29 for (int i = 0; i < WARM_UP_ITERATION_COUNT; i++) {30 assertThatCartoonCharactersWithNullAndEmptyAreValid(CARTOON_CHARACTERS_WITH_NULL_AND_EMPTY);31 }32 System.out.println("Warm up for AssertJ assertions");33 for (int i = 0; i <
CartoonCharacter
Using AI Code Generation
1@org.openjdk.jmh.annotations.State(org.openjdk.jmh.annotations.Scope.Benchmark)2public static class SoftAssertionsPerfTest_assertThat_with_soft_assertions {3 public void setUp() throws java.lang.Exception {4 org.assertj.core.api.SoftAssertions softly = new org.assertj.core.api.SoftAssertions();5 org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter homer = new org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter("Homer", 39);6 org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter marge = new org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter("Marge", 36);7 org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter bart = new org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter("Bart", 10);8 org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter lisa = new org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter("Lisa", 8);9 org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter maggie = new org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter("Maggie", 1);10 org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter abraham = new org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter("Abraham", 107);11 org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter mona = new org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter("Mona", 107);12 org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter waylon = new org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter("Waylon", 107);13 org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter krusty = new org.assertj.core.tests.perf.SoftAssertionsPerfTest.CartoonCharacter("Krusty", 107);
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!!