Best Assertj code snippet using org.assertj.core.internal.StandardComparisonStrategy_areEqual_Test.argument
Source:StandardComparisonStrategy_areEqual_Test.java
...12 */13package org.assertj.core.internal;14import static org.assertj.core.api.Assertions.catchThrowable;15import static org.assertj.core.api.BDDAssertions.then;16import static org.junit.jupiter.params.provider.Arguments.arguments;17import java.util.stream.Stream;18import org.junit.jupiter.api.Test;19import org.junit.jupiter.params.ParameterizedTest;20import org.junit.jupiter.params.provider.Arguments;21import org.junit.jupiter.params.provider.MethodSource;22/**23 * @author Joel Costigliola24 */25class StandardComparisonStrategy_areEqual_Test {26 private final StandardComparisonStrategy underTest = StandardComparisonStrategy.instance();27 @Test28 void should_return_true_if_both_actual_and_other_are_null() {29 // WHEN30 boolean result = underTest.areEqual(null, null);31 // THEN32 then(result).isTrue();33 }34 @Test35 void should_return_false_if_actual_is_null_and_other_is_not() {36 // GIVEN37 Object other = new Object();38 // WHEN39 boolean result = underTest.areEqual(null, other);40 // THEN41 then(result).isFalse();42 }43 @Test44 void should_return_true_if_Object_arrays_are_equal() {45 // GIVEN46 Object[] actual = { "Luke", "Yoda", "Leia" };47 Object[] other = { "Luke", "Yoda", "Leia" };48 // WHEN49 boolean result = underTest.areEqual(actual, other);50 // THEN51 then(result).isTrue();52 }53 @Test54 void should_return_false_if_Object_arrays_are_not_equal() {55 // GIVEN56 Object[] actual = { "Luke", "Leia", "Yoda" };57 Object[] other = { "Luke", "Yoda", "Leia" };58 // WHEN59 boolean result = underTest.areEqual(actual, other);60 // THEN61 then(result).isFalse();62 }63 @Test64 void should_return_true_if_byte_arrays_are_equal() {65 // GIVEN66 byte[] actual = { 1, 2, 3 };67 byte[] other = { 1, 2, 3 };68 // WHEN69 boolean result = underTest.areEqual(actual, other);70 // THEN71 then(result).isTrue();72 }73 @Test74 void should_return_false_if_byte_arrays_are_not_equal() {75 // GIVEN76 byte[] actual = { 1, 2, 3 };77 byte[] other = { 4, 5, 6 };78 // WHEN79 boolean result = underTest.areEqual(actual, other);80 // THEN81 then(result).isFalse();82 }83 @Test84 void should_return_true_if_short_arrays_are_equal() {85 // GIVEN86 short[] actual = { 1, 2, 3 };87 short[] other = { 1, 2, 3 };88 // WHEN89 boolean result = underTest.areEqual(actual, other);90 // THEN91 then(result).isTrue();92 }93 @Test94 void should_return_false_if_short_arrays_are_not_equal() {95 // GIVEN96 short[] actual = { 1, 2, 3 };97 short[] other = { 4, 5, 6 };98 // WHEN99 boolean result = underTest.areEqual(actual, other);100 // THEN101 then(result).isFalse();102 }103 @Test104 void should_return_true_if_int_arrays_are_equal() {105 // GIVEN106 int[] actual = { 1, 2, 3 };107 int[] other = { 1, 2, 3 };108 // WHEN109 boolean result = underTest.areEqual(actual, other);110 // THEN111 then(result).isTrue();112 }113 @Test114 void should_return_false_if_int_arrays_are_not_equal() {115 // GIVEN116 int[] actual = { 1, 2, 3 };117 int[] other = { 4, 5, 6 };118 // WHEN119 boolean result = underTest.areEqual(actual, other);120 // THEN121 then(result).isFalse();122 }123 @Test124 void should_return_true_if_long_arrays_are_equal() {125 // GIVEN126 long[] actual = { 1L, 2L, 3L };127 long[] other = { 1L, 2L, 3L };128 // WHEN129 boolean result = underTest.areEqual(actual, other);130 // THEN131 then(result).isTrue();132 }133 @Test134 void should_return_false_if_long_arrays_are_not_equal() {135 // GIVEN136 long[] actual = { 1L, 2L, 3L };137 long[] other = { 4L, 5L, 6L };138 // WHEN139 boolean result = underTest.areEqual(actual, other);140 // THEN141 then(result).isFalse();142 }143 @Test144 void should_return_true_if_char_arrays_are_equal() {145 // GIVEN146 char[] actual = { '1', '2', '3' };147 char[] other = { '1', '2', '3' };148 // WHEN149 boolean result = underTest.areEqual(actual, other);150 // THEN151 then(result).isTrue();152 }153 @Test154 void should_return_false_if_char_arrays_are_not_equal() {155 // GIVEN156 char[] actual = { '1', '2', '3' };157 char[] other = { '4', '5', '6' };158 // WHEN159 boolean result = underTest.areEqual(actual, other);160 // THEN161 then(result).isFalse();162 }163 @Test164 void should_return_true_if_float_arrays_are_equal() {165 // GIVEN166 float[] actual = { 1.0f, 2.0f, 3.0f };167 float[] other = { 1.0f, 2.0f, 3.0f };168 // WHEN169 boolean result = underTest.areEqual(actual, other);170 // THEN171 then(result).isTrue();172 }173 @Test174 void should_return_false_if_float_arrays_are_not_equal() {175 // GIVEN176 float[] actual = { 1.0f, 2.0f, 3.0f };177 float[] other = { 4.0f, 5.0f, 6.0f };178 // WHEN179 boolean result = underTest.areEqual(actual, other);180 // THEN181 then(result).isFalse();182 }183 @Test184 void should_return_true_if_double_arrays_are_equal() {185 // GIVEN186 double[] actual = { 1.0, 2.0, 3.0 };187 double[] other = { 1.0, 2.0, 3.0 };188 // WHEN189 boolean result = underTest.areEqual(actual, other);190 // THEN191 then(result).isTrue();192 }193 @Test194 void should_return_false_if_double_arrays_are_not_equal() {195 // GIVEN196 double[] actual = { 1.0, 2.0, 3.0 };197 double[] other = { 4.0, 5.0, 6.0 };198 // WHEN199 boolean result = underTest.areEqual(actual, other);200 // THEN201 then(result).isFalse();202 }203 @Test204 void should_return_true_if_boolean_arrays_are_equal() {205 // GIVEN206 boolean[] actual = { true, false };207 boolean[] other = { true, false };208 // WHEN209 boolean result = underTest.areEqual(actual, other);210 // THEN211 then(result).isTrue();212 }213 @Test214 void should_return_false_if_boolean_arrays_are_not_equal() {215 // GIVEN216 boolean[] actual = { true, false };217 boolean[] other = { false, true };218 // WHEN219 boolean result = underTest.areEqual(actual, other);220 // THEN221 then(result).isFalse();222 }223 @ParameterizedTest224 @MethodSource("arrays")225 void should_return_false_if_array_is_non_null_and_other_is_null(Object actual) {226 // WHEN227 boolean result = underTest.areEqual(actual, null);228 // THEN229 then(result).isFalse();230 }231 private static Stream<Object> arrays() {232 return Stream.of(argument(new Object[] { "Luke", "Yoda", "Leia" }),233 new byte[] { 1, 2, 3 },234 new short[] { 1, 2, 3 },235 new int[] { 1, 2, 3 },236 new long[] { 1L, 2L, 3L },237 new char[] { '1', '2', '3' },238 new float[] { 1.0f, 2.0f, 3.0f },239 new double[] { 1.0, 2.0, 3.0 },240 new boolean[] { true, false });241 }242 // Arrays of objects require additional wrapping to avoid expanding into individual elements.243 // See https://github.com/junit-team/junit5/issues/1665 and https://github.com/junit-team/junit5/issues/2708244 private static Arguments argument(Object[] array) {245 return () -> new Object[] { array };246 }247 @Test248 void should_fail_if_equals_implementation_fails() {249 // GIVEN250 Object actual = new EqualsThrowsException();251 // WHEN252 Throwable thrown = catchThrowable(() -> underTest.areEqual(actual, new Object()));253 // THEN254 then(thrown).isInstanceOf(RuntimeException.class)255 .hasMessage("Boom!");256 }257 private static class EqualsThrowsException {258 @Override259 public boolean equals(Object obj) {260 throw new RuntimeException("Boom!");261 }262 }263 @ParameterizedTest264 @MethodSource({ "correctEquals", "contractViolatingEquals" })265 void should_delegate_to_equals_implementation_if_actual_is_not_null(Object actual, Object other, boolean expected) {266 // WHEN267 boolean result = underTest.areEqual(actual, other);268 // THEN269 then(result).isEqualTo(expected);270 }271 // not part of contractViolatingEquals due to test order dependency272 @Test273 void should_delegate_to_inconsistent_equals_implementation() {274 // GIVEN275 Object actual = new NonConsistent();276 // WHEN277 boolean[] results = {278 underTest.areEqual(actual, actual),279 underTest.areEqual(actual, actual),280 underTest.areEqual(actual, actual)281 };282 // THEN283 then(results).containsExactly(true, false, true);284 }285 private static Stream<Arguments> correctEquals() {286 Object object = new Object();287 return Stream.of(arguments(object, null, false),288 arguments(object, object, true),289 arguments(object, new Object(), false),290 arguments("Luke", null, false),291 arguments("Luke", "Luke", true),292 arguments("Luke", "Yoda", false));293 }294 private static Stream<Arguments> contractViolatingEquals() {295 AlwaysTrue alwaysTrue = new AlwaysTrue();296 AlwaysFalse alwaysFalse = new AlwaysFalse();297 NonReflexive nonReflexiveX = new NonReflexive();298 NonSymmetric nonSymmetricY = new NonSymmetric(null);299 NonSymmetric nonSymmetricX = new NonSymmetric(nonSymmetricY);300 NonTransitive nonTransitiveZ = new NonTransitive(null, null);301 NonTransitive nonTransitiveY = new NonTransitive(nonTransitiveZ, null);302 NonTransitive nonTransitiveX = new NonTransitive(nonTransitiveY, nonTransitiveZ);303 return Stream.of(arguments(alwaysTrue, null, true),304 arguments(alwaysFalse, alwaysFalse, false),305 arguments(nonReflexiveX, nonReflexiveX, false),306 arguments(nonSymmetricX, nonSymmetricY, true),307 arguments(nonSymmetricY, nonSymmetricX, false),308 arguments(nonTransitiveX, nonTransitiveY, true),309 arguments(nonTransitiveY, nonTransitiveZ, true),310 arguments(nonTransitiveX, nonTransitiveZ, false));311 }312 private static class AlwaysTrue {313 @Override314 public boolean equals(Object obj) {315 return true;316 }317 @Override318 public String toString() {319 return "always true";320 }321 }322 private static class AlwaysFalse {323 @Override324 public boolean equals(Object obj) {...
argument
Using AI Code Generation
1package org.assertj.core.internal;2import static org.assertj.core.error.ShouldBeEqual.shouldBeEqual;3import static org.assertj.core.test.ExpectedException.none;4import static org.assertj.core.test.TestData.someInfo;5import static org.assertj.core.util.FailureMessages.actualIsNull;6import static org.assertj.core.util.Objects.areEqual;7import static org.assertj.core.util.Strings.concat;8import static org.mockito.Mockito.verify;9import org.assertj.core.api.AssertionInfo;10import org.assertj.core.test.ExpectedException;11import org.junit.Before;12import org.junit.Rule;13import org.junit.Test;14public class StandardComparisonStrategy_areEqual_Test {15 public ExpectedException thrown = none();16 private StandardComparisonStrategy comparisonStrategy;17 public void setUp() {18 comparisonStrategy = StandardComparisonStrategy.instance();19 }20 public void should_throw_error_if_actual_is_null() {21 thrown.expectAssertionError(actualIsNull());22 comparisonStrategy.areEqual(someInfo(), null, "Yoda");23 }24 public void should_return_true_if_both_Objects_are_null() {25 comparisonStrategy.areEqual(someInfo(), null, null);26 }27 public void should_return_true_if_Objects_are_equal() {28 comparisonStrategy.areEqual(someInfo(), "Yoda", "Yoda");29 }30 public void should_return_false_if_Objects_are_not_equal() {31 comparisonStrategy.areEqual(someInfo(), "Yoda", "Luke");32 }33 public void should_return_true_if_Objects_are_equal_according_to_custom_comparison_strategy() {34 comparisonStrategy.areEqual(someInfo(), "Yoda", "YODA");35 }36 public void should_return_false_if_Objects_are_not_equal_according_to_custom_comparison_strategy() {37 comparisonStrategy.areEqual(someInfo(), "Yoda", "Luke");38 }39 public void should_fail_if_objects_are_not_equal_and_custom_comparison_strategy_is_used() {40 AssertionInfo info = someInfo();41 try {42 comparisonStrategy.areEqual(info, "Yoda", "Luke");43 }
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!!