How to use compare method of org.assertj.core.api.ComparatorFactory class

Best Assertj code snippet using org.assertj.core.api.ComparatorFactory.compare

Source:ComparatorFactory.java Github

copy

Full Screen

...20 public Comparator<Double> doubleComparatorWithPrecision(double precision) {21 // can't use <> with anonymous class in java 822 return new Comparator<Double>() {23 @Override24 public int compare(Double double1, Double double2) {25 if (Doubles.instance().isNanOrInfinite(precision)) {26 throw new IllegalArgumentException("Precision should not be Nan or Infinity!");27 }28 // handle NAN or Infinity cases with Java Float behavior (and not BigDecimal that are used afterwards)29 if (Doubles.instance().isNanOrInfinite(double1) || Doubles.instance().isNanOrInfinite(double2)) {30 return Double.compare(double1, double2);31 }32 // if floats are close enough they are considered equal, otherwise we compare as BigDecimal which does exact computation.33 return isWithinPrecision(double1, double2, precision) ? 0 : asBigDecimal(double1).compareTo(asBigDecimal(double2));34 }35 @Override36 public String toString() {37 return "double comparator at precision " + precision + " (values are considered equal if diff == precision)";38 }39 };40 }41 public Comparator<Float> floatComparatorWithPrecision(float precision) {42 // can't use <> with anonymous class in java 843 return new Comparator<Float>() {44 @Override45 public int compare(Float float1, Float float2) {46 Floats floats = Floats.instance();47 if (floats.isNanOrInfinite(precision)) {48 throw new IllegalArgumentException("Precision should not be Nan or Infinity!");49 }50 // handle NAN or Infinity cases with Java Float behavior (and not BigDecimal that are used afterwards)51 if (floats.isNanOrInfinite(float1) || floats.isNanOrInfinite(float2)) {52 return Float.compare(float1, float2);53 }54 // if floats are close enough they are considered equal, otherwise we compare as BigDecimal which does exact computation.55 return isWithinPrecision(float1, float2, precision) ? 0 : asBigDecimal(float1).compareTo(asBigDecimal(float2));56 }57 @Override58 public String toString() {59 return "float comparator at precision " + precision + " (values are considered equal if diff == precision)";60 }61 };62 }63 /**64 * Convert to a precise BigDecimal object using an intermediate String.65 *66 * @param <T> type of expected and precision, which should be the subclass of java.lang.Number and java.lang.Comparable67 * @param number the Number to convert68 * @return the built BigDecimal69 */70 private static <T extends Number> BigDecimal asBigDecimal(T number) {71 return new BigDecimal(String.valueOf(number));72 }73 /**74 * Returns true if the abs(expected - precision) is &lt;= precision, false otherwise.75 * @param actual the actual value76 * @param expected the expected value77 * @param precision the acceptable precision78 *79 * @param <T> type of number to compare including the precision80 * @return whether true if the abs(expected - precision) is &lt;= precision, false otherwise.81 */82 private static <T extends Number> boolean isWithinPrecision(T actual, T expected, T precision) {83 BigDecimal expectedBigDecimal = asBigDecimal(expected);84 BigDecimal actualBigDecimal = asBigDecimal(actual);85 BigDecimal absDifference = expectedBigDecimal.subtract(actualBigDecimal).abs();86 BigDecimal precisionAsBigDecimal = asBigDecimal(precision);87 return absDifference.compareTo(precisionAsBigDecimal) <= 0;88 }89}...

Full Screen

Full Screen

Source:ComparatorFactory_doubleComparatorWithPrecision_Test.java Github

copy

Full Screen

...35 void should_evaluate_to_be_equal(Double double1, Double double2, Double precision) {36 // GIVEN37 Comparator<Double> comparator = INSTANCE.doubleComparatorWithPrecision(precision);38 // WHEN39 int comparisonValue = comparator.compare(double1, double2);40 int inverseComparisonValue = comparator.compare(double2, double1);41 // THEN42 then(comparisonValue).isZero();43 then(inverseComparisonValue).isZero();44 }45 @ParameterizedTest46 @CsvSource({47 "1.1, 1.0, 0.05",48 "0.111, 0.110, 0.00099",49 "0.12346, 0.12345, 0.0000099",50 "0.7654321, 0.7654320, 0.000000099",51 "0.7654321, 0.7654320, 9e-8",52 "1.2464, 1.2463, 0.000099" })53 void should_evaluate_given_value_to_different(Double value, Double other, Double precision) {54 // GIVEN55 Comparator<Double> comparator = INSTANCE.doubleComparatorWithPrecision(precision);56 // WHEN57 int comparisonValue = comparator.compare(value, other);58 int inverseComparisonValue = comparator.compare(other, value);59 // THEN60 then(comparisonValue).isOne();61 then(inverseComparisonValue).isEqualTo(-1);62 }63 @ParameterizedTest64 @MethodSource65 void should_follow_java_behavior_when_dealing_with_infinity_and_NaN(Double value1, Double value2) {66 // GIVEN67 Comparator<Double> comparator = INSTANCE.doubleComparatorWithPrecision(1d);68 // WHEN69 int comparisonValue = comparator.compare(value1, value2);70 int javaComparisonValue = value1.compareTo(value2);71 // THEN72 then(comparisonValue).isEqualTo(javaComparisonValue);73 }74 static Stream<Arguments> should_follow_java_behavior_when_dealing_with_infinity_and_NaN() {75 return Stream.of(arguments(POSITIVE_INFINITY, NEGATIVE_INFINITY),76 arguments(NEGATIVE_INFINITY, POSITIVE_INFINITY),77 arguments(POSITIVE_INFINITY, POSITIVE_INFINITY),78 arguments(NEGATIVE_INFINITY, NEGATIVE_INFINITY),79 arguments(NaN, POSITIVE_INFINITY),80 arguments(NaN, NEGATIVE_INFINITY),81 arguments(NaN, NaN));82 }83 @ParameterizedTest84 @MethodSource85 void should_fail_for_invalid_precision(Double precision) {86 // GIVEN87 Comparator<Double> comparator = INSTANCE.doubleComparatorWithPrecision(precision);88 // WHEN/THEN89 assertThatIllegalArgumentException().isThrownBy(() -> comparator.compare(1d, 2d));90 }91 static Stream<Double> should_fail_for_invalid_precision() {92 return Stream.of(NaN, POSITIVE_INFINITY, NEGATIVE_INFINITY);93 }94}...

Full Screen

Full Screen

Source:ComparatorFactory_floatComparatorWithPrecision_Test.java Github

copy

Full Screen

...35 void should_evaluate_to_be_equal(Float float1, Float float2, Float precision) {36 // GIVEN37 Comparator<Float> comparator = INSTANCE.floatComparatorWithPrecision(precision);38 // WHEN39 int comparisonValue = comparator.compare(float1, float2);40 int inverseComparisonValue = comparator.compare(float2, float1);41 // THEN42 then(comparisonValue).isZero();43 then(inverseComparisonValue).isZero();44 }45 @ParameterizedTest46 @CsvSource({47 "1.1, 1.0, 0.05",48 "0.111, 0.110, 0.00099",49 "0.12346, 0.12345, 0.0000099",50 "0.7654321, 0.7654320, 0.000000099",51 "0.7654321, 0.7654320, 9e-8",52 "1.2464, 1.2463, 0.000099" })53 void should_evaluate_given_value_to_different(Float value, Float other, Float precision) {54 // GIVEN55 Comparator<Float> comparator = INSTANCE.floatComparatorWithPrecision(precision);56 // WHEN57 int comparisonValue = comparator.compare(value, other);58 int inverseComparisonValue = comparator.compare(other, value);59 // THEN60 then(comparisonValue).isOne();61 then(inverseComparisonValue).isEqualTo(-1);62 }63 @ParameterizedTest64 @MethodSource65 void should_follow_java_behavior_when_dealing_with_infinity_and_NaN(Float value1, Float value2) {66 // GIVEN67 Comparator<Float> comparator = INSTANCE.floatComparatorWithPrecision(1f);68 // WHEN69 int comparisonValue = comparator.compare(value1, value2);70 int javaComparisonValue = value1.compareTo(value2);71 // THEN72 then(comparisonValue).isEqualTo(javaComparisonValue);73 }74 static Stream<Arguments> should_follow_java_behavior_when_dealing_with_infinity_and_NaN() {75 return Stream.of(arguments(POSITIVE_INFINITY, NEGATIVE_INFINITY),76 arguments(NEGATIVE_INFINITY, POSITIVE_INFINITY),77 arguments(POSITIVE_INFINITY, POSITIVE_INFINITY),78 arguments(NEGATIVE_INFINITY, NEGATIVE_INFINITY),79 arguments(NaN, POSITIVE_INFINITY),80 arguments(NaN, NEGATIVE_INFINITY),81 arguments(NaN, NaN));82 }83 @ParameterizedTest84 @MethodSource85 void should_fail_for_invalid_precision(Float precision) {86 // GIVEN87 Comparator<Float> comparator = INSTANCE.floatComparatorWithPrecision(precision);88 // WHEN/THEN89 assertThatIllegalArgumentException().isThrownBy(() -> comparator.compare(1f, 2f));90 }91 static Stream<Float> should_fail_for_invalid_precision() {92 return Stream.of(NaN, POSITIVE_INFINITY, NEGATIVE_INFINITY);93 }94}...

Full Screen

Full Screen

compare

Using AI Code Generation

copy

Full Screen

1package org.example;2import java.util.Comparator;3import org.assertj.core.api.AbstractAssert;4import org.assertj.core.api.ComparatorFactory;5import org.assertj.core.api.IterableAssert;6import org.assertj.core.api.ObjectAssert;7import org.assertj.core.api.ObjectAssertFactory;8import org.assertj.core.api.ObjectArrayAssert;9import org.assertj.core.api.ObjectArrayAssertFactory;10import org.assertj.core.api.ObjectEnumerableAssert;11import org.assertj.core.api.ObjectEnumerableAssertFactory;12import org.assertj.core.api.ObjectEnumerableAssertBase;13import org.assertj.core.api.ObjectEnumerableAssertBaseTest;14import org.assertj.core.api.ObjectListAssert;15import org.assertj.core.api.ObjectListAssertFactory;16import org.assertj.core.api.ObjectSetAssert;17import org.assertj.core.api.ObjectSetAssertFactory;18import org.assertj.core.api.ObjectSortedSetAssert;19import org.assertj.core.api.ObjectSortedSetAssertFactory;20import org.assertj.core.api.ThrowableAssert;21import org.assertj.core.api.ThrowableAssertFactory;22import org.assertj.core.api.ThrowableAssertAlternative;23import org.assertj.core.api.ThrowableAssertAlternativeFactory;24import org.assertj.core.api.ThrowableAssertBase;25import org.assertj.core.api.ThrowableAssertBaseTest;26import org.assertj.core.api.ThrowableAssertNoCause;27import org.assertj.core.api.ThrowableAssertNoCauseFactory;28import org.assertj.core.api.ThrowableAssertNoCauseAlternative;29import org.assertj.core.api.ThrowableAssertNoCauseAlternativeFactory;30import org.assertj.core.api.ThrowableAssertNoCauseBase;31import org.assertj.core.api.ThrowableAssertNoCauseBaseTest;32import org.assertj.core.api.ThrowableAssertWithCause;33import org.assertj.core.api.ThrowableAssertWithCauseFactory;34import org.assertj.core.api.ThrowableAssertWithCauseAlternative;35import org.assertj.core.api.ThrowableAssertWithCauseAlternativeFactory;36import org.assertj.core.api.ThrowableAssertWithCauseBase;37import org.assertj.core.api.ThrowableAssertWithCauseBaseTest;38import org.assertj.core.api.ThrowableAssertWithMessage;39import org.assertj.core.api.ThrowableAssertWithMessageFactory;40import org.assertj.core.api.ThrowableAssertWithMessageAlternative;41import org.assertj.core.api.ThrowableAssertWithMessageAlternativeFactory;42import org.assertj.core.api.ThrowableAssertWithMessageBase;43import org.assertj.core.api.ThrowableAssertWithMessageBaseTest;44import org.assertj.core.api.ThrowableAssertWithMessageNoCause;45import org.assertj.core.api.ThrowableAssertWithMessageNoCauseFactory;46import org.assertj.core.api.ThrowableAssertWithMessageNoCauseAlternative;47import org.assertj.core.api.ThrowableAssert

Full Screen

Full Screen

compare

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.api;2import java.util.Comparator;3import org.assertj.core.internal.ComparatorBasedComparisonStrategy;4import org.assertj.core.internal.ComparisonStrategy;5public class ComparatorFactory {6 public static ComparatorBasedComparisonStrategy comparatorBasedComparisonStrategy(Comparator<?> comparator) {7 return new ComparatorBasedComparisonStrategy(comparator);8 }9 public static ComparisonStrategy comparatorBasedComparisonStrategy(Comparator<?> comparator, boolean reversed) {10 return new ComparatorBasedComparisonStrategy(comparator, reversed);11 }12}13package org.assertj.core.api;14import java.util.Comparator;15import org.assertj.core.internal.ComparatorBasedComparisonStrategy;16import org.assertj.core.internal.ComparisonStrategy;17public class ComparatorBasedComparisonStrategy implements ComparisonStrategy {18 private final Comparator<?> comparator;19 private final boolean reversed;20 public ComparatorBasedComparisonStrategy(Comparator<?> comparator) {21 this(comparator, false);22 }23 public ComparatorBasedComparisonStrategy(Comparator<?> comparator, boolean reversed) {24 this.comparator = comparator;25 this.reversed = reversed;26 }27 public int compare(Object o1, Object o2) {28 if (reversed) {29 return comparator.compare(o2, o1);30 }31 return comparator.compare(o1, o2);32 }33 public boolean equals(Object o1, Object o2) {34 return compare(o1, o2) == 0;35 }36 public int hashCodeOf(Object o) {37 return o.hashCode();38 }39 public boolean isOrderMaintained() {40 return true;41 }42 public boolean isTotalOrdering() {43 return false;44 }45 public String toStringOf(Object o) {46 return o.toString();47 }48}49package org.assertj.core.api;50import java.util.Comparator;51import org.assertj.core.internal.ComparatorBasedComparisonStrategy;52import org.assertj.core.internal.ComparisonStrategy;53public class Assertions {54 public static ComparatorBasedComparisonStrategy comparatorBasedComparisonStrategy(Comparator<?> comparator) {55 return new ComparatorBasedComparisonStrategy(comparator);56 }57 public static ComparisonStrategy comparatorBasedComparisonStrategy(Comparator<?> comparator, boolean reversed) {58 return new ComparatorBasedComparisonStrategy(comparator, reversed);59 }60}61package org.assertj.core.api;62import java.util.Comparator;63import org.assertj.core.internal.ComparatorBasedComparisonStrategy

Full Screen

Full Screen

compare

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.ComparatorFactory;2import java.util.Comparator;3class Student {4 int id;5 String name;6 String address;7 public Student(int id, String name, String address) {8 this.id = id;9 this.name = name;10 this.address = address;11 }12 public int getId() {13 return id;14 }15 public String getName() {16 return name;17 }18 public String getAddress() {19 return address;20 }21}22public class AssertJComparator {23 public static void main(String[] args) {24 Student student1 = new Student(1, "John", "USA");25 Student student2 = new Student(1, "John", "USA");26 Comparator<Student> comparator = ComparatorFactory.comparator().onProperty(Student::getId).thenComparing(Student::getName).thenComparing(Student::getAddress);27 System.out.println(comparator.compare(student1, student2));28 }29}

Full Screen

Full Screen

compare

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.ComparatorFactory;2import org.assertj.core.api.Assertions;3import org.junit.Test;4public class 1 {5 public void test() {6 Assertions.assertThat("abc").usingComparator(ComparatorFactory.comparator()).isEqualTo("abc");7 }8}9 at org.junit.Assert.assertEquals(Assert.java:115)10 at org.junit.Assert.assertEquals(Assert.java:144)11 at 1.test(1.java:10)

Full Screen

Full Screen

compare

Using AI Code Generation

copy

Full Screen

1public class Compare {2 public static void main(String[] args) {3 int i = 10;4 int j = 20;5 assertThat(i).usingComparator(ComparatorFactory.<Integer>compare()).isLessThan(j);6 }7}8public class Compare {9 public static void main(String[] args) {10 int i = 10;11 int j = 20;12 assertThat(i).usingComparator(Assertions.<Integer>compare()).isLessThan(j);13 }14}15public class Compare {16 public static void main(String[] args) {17 int i = 10;18 int j = 20;19 assertThat(i).usingComparator(Assertions.<Integer>compare()).isLessThan(j);20 }21}22public class Compare {23 public static void main(String[] args) {24 int i = 10;25 int j = 20;26 assertThat(i).usingComparator(Assertions.<Integer>compare()).isLessThan(j);27 }28}29public class Compare {30 public static void main(String[] args) {31 int i = 10;32 int j = 20;33 assertThat(i).usingComparator(Assertions.<Integer>compare()).isLessThan(j);34 }35}36public class Compare {37 public static void main(String[] args) {38 int i = 10;39 int j = 20;40 assertThat(i).usingComparator(Assertions.<Integer>compare()).isLessThan(j);41 }42}

Full Screen

Full Screen

compare

Using AI Code Generation

copy

Full Screen

1import java.util.Comparator;2import org.assertj.core.api.ComparatorFactory;3import org.assertj.core.api.ComparisonStrategy;4import org.assertj.core.api.DefaultComparator;5import org.assertj.core.api.ObjectAssert;6public class ComparatorFactoryDemo {7 public static void main(String[] args) {8 ObjectAssert object1 = new ObjectAssert("AssertJ");9 ObjectAssert object2 = new ObjectAssert("AssertJ");10 Comparator comparator1 = new DefaultComparator();11 Comparator comparator2 = new DefaultComparator();12 ComparisonStrategy comparisonStrategy1 = new ComparisonStrategy();13 ComparisonStrategy comparisonStrategy2 = new ComparisonStrategy();14 ComparatorFactory comparatorFactory1 = new ComparatorFactory();15 ComparatorFactory comparatorFactory2 = new ComparatorFactory();16 comparatorFactory1.compare(object1, object2);17 comparatorFactory2.compare(comparator1, comparator2);18 comparatorFactory1.compare(comparisonStrategy1, comparisonStrategy2);19 }20}

Full Screen

Full Screen

compare

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.ComparatorFactory;2import org.assertj.core.api.ComparableAssert;3import org.assertj.core.api.ComparableAssertBaseTest;4import org.assertj.core.api.Condition;5import org.assertj.core.api.TestCondition;6import org.assertj.core.description.TextDescription;7import org.assertj.core.internal.ComparatorBasedComparisonStrategy;8import org.assertj.core.internal.Comparables;9import org.assertj.core.internal.Comparators;10import org.assertj.core.internal.Failures;11import org.assertj.core.internal.Objects;12import org.assertj.core.util.AbsValueComparator;13import java.util.Comparator;14import java.util.function.BiPredicate;15import static org.assertj.core.api.Assertions.assertThat;16import static org.assertj.core.api.Assertions.assertThatExceptionOfType;17import static org.assertj.core.api.Assertions.assertThatNullPointerException;18import static org.assertj.core.api.Assertions.not;19import static org.assertj.core.api.Assertions.within;20import static org.assertj.core.api.BDDAssertions.then;21import static org.assertj.core.error.ShouldBeEqual.shouldBeEqual;22import static org.assertj.core.error.ShouldBeEqualWithinOffset.shouldBeEqual;23import static org.assertj.core.error.ShouldBeGreaterThan.shouldBeGreaterThan;24import static org.assertj.core.error.ShouldBeGreaterThanOrEqualTo.shouldBeGreaterThanOrEqualTo;25import static org.assertj.core.error.ShouldBeLessThan.shouldBeLessThan;26import static org.assertj.core.error.ShouldBeLessThanOrEqualTo.shouldBeLessThanOrEqualTo;27import static org.assertj.core.error.ShouldBeNegative.shouldBeNegative;28import static org.assertj.core.error.ShouldBeNotEqual.shouldBeNotEqual;29import static org.assertj.core.error.ShouldBeNotEqualByComparingTo.shouldBeEqual;30import static org.assertj.core.error.ShouldBePositive.shouldBePositive;31import static org.assertj.core.error.ShouldHaveValue.shouldHaveValue;32import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;33import static org.assertj.core.error.ShouldSatisfy.shouldSatisfy;34import static org.assertj.core.util.AssertionsUtil.expectAssertionError;35import static org.assertj.core.util.FailureMessages.actualIsNull;36import static org.assertj.core.util.Lists.newArrayList;37import static org.mockito.Mockito.verify;38import static org.mockito.Mockito.verifyNoMoreInteractions;39import static org.mockito.Mockito.when;40import org.assertj.core.api.AbstractComparableAssert;41import org.assertj.core.api.AbstractIntegerAssert;42import org.assertj.core.api.AbstractObjectAssert;43import org.assertj.core.api.AbstractStringAssert;44import org.assertj.core.api.Assertions;45import org.assertj.core.api.Condition;46import org.assertj.core.api.IntegerAssert;47import org.assertj.core.api.ObjectAssert;48import org.assertj.core.api.StringAssert;49import org.assertj.core.api.ThrowableAssert

Full Screen

Full Screen

compare

Using AI Code Generation

copy

Full Screen

1import java.util.Comparator;2import org.assertj.core.api.Assertions;3import org.assertj.core.api.ComparatorFactory;4public class CompareMethod {5public static void main(String[] args) {6Comparator<String> comparator = ComparatorFactory.comparable();7Assertions.assertThat("b").usingComparator(comparator).isGreaterThan("a");8Assertions.assertThat("a").usingComparator(comparator).isLessThan("b");9Assertions.assertThat("a").usingComparator(comparator).isGreaterThanOrEqualTo("a");10Assertions.assertThat("b").usingComparator(comparator).isLessThanOrEqualTo("b");11Assertions.assertThat("a").usingComparator(comparator).isGreaterThan("b");12Assertions.assertThat("b").usingComparator(comparator).isLessThan("a");13Assertions.assertThat("a").usingComparator(comparator).isGreaterThanOrEqualTo("b");14Assertions.assertThat("b").usingComparator(comparator).isLessThanOrEqualTo("a");15}16}

Full Screen

Full Screen

compare

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.ComparatorFactory.byLessThan;3import java.util.Date;4import org.junit.Test;5public class DateAssertJTest {6 public void testDateCompare() {7 Date date1 = new Date(2018, 12, 31);8 Date date2 = new Date(2019, 01, 01);9 assertThat(date1).usingComparator(byLessThan()).isLessThan(date2);10 }11}12import static org.assertj.core.api.Assertions.assertThat;13import static org.assertj.core.api.ComparatorFactory.byComparator;14import java.util.Date;15import java.util.Comparator;16import org.junit.Test;17public class DateAssertJTest {18 public void testDateCompare() {19 Date date1 = new Date(2018, 12, 31);20 Date date2 = new Date(2019, 01, 01);21 assertThat(date1).usingComparator(byComparator(new Comparator<Date>() {22 public int compare(Date o1, Date o2) {23 return o1.compareTo(o2);24 }25 })).isLessThan(date2);26 }27}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Assertj automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful