From 6f496d0d5c099809502351f325439e8fea1db33a Mon Sep 17 00:00:00 2001 From: Marcin Krykowski Date: Wed, 27 May 2020 16:34:21 +0100 Subject: [PATCH] Restructured test using AsserJ library --- .../baeldung/formatNumber/FormatNumber.java | 24 --------- .../formatNumber/FormatNumberTest.java | 51 +++++++++---------- 2 files changed, 23 insertions(+), 52 deletions(-) diff --git a/java-numbers/src/main/java/com/baeldung/formatNumber/FormatNumber.java b/java-numbers/src/main/java/com/baeldung/formatNumber/FormatNumber.java index a9925dee9d..a0a9e05e83 100644 --- a/java-numbers/src/main/java/com/baeldung/formatNumber/FormatNumber.java +++ b/java-numbers/src/main/java/com/baeldung/formatNumber/FormatNumber.java @@ -7,30 +7,6 @@ import java.text.NumberFormat; import java.util.Locale; public class FormatNumber { - private static final double D = 4.2352989244d; - private static final double F = 8.6994540927d; - - public static void main(String[] args) { - System.out.println(D + " with Big Decimal (2 places) is " + withBigDecimal(D, 2)); - System.out.println(D + " with Big Decimal (3 places) is " + withBigDecimal(D, 3)); - System.out.println(F + " with Big Decimal (2 places) is " + withBigDecimal(F, 2)); - System.out.println(F + " with Big Decimal (3 places) is " + withBigDecimal(F, 3)); - System.out.println(D + " with Math.round is (2 places) " + withMathRound(D, 2)); - System.out.println(D + " with Math.round is (3 places) " + withMathRound(D, 3)); - System.out.println(F + " with Math.round is (2 places) " + withMathRound(F, 2)); - System.out.println(F + " with Math.round is (3 places) " + withMathRound(F, 3)); - System.out.println(D + " with String Format is (2 places) " + withStringFormat(D, 2)); - System.out.println(D + " with String Format is (3 places) " + withStringFormat(D, 3)); - System.out.println(F + " with String Format is (2 places) " + withStringFormat(F, 2)); - System.out.println(F + " with String Format is (3 places) " + withStringFormat(F, 3)); - System.out.println(D + " with Decimal Format (local) is " + withDecimalFormatLocal(D)); - System.out.println(D + " with Decimal Format (2 places) is " + withDecimalFormatPattern(D, 2)); - System.out.println(D + " with Decimal Format (3 places) is " + withDecimalFormatPattern(D, 3)); - System.out.println(F + " with Decimal Format is (local) " + withDecimalFormatLocal(F)); - System.out.println(F + " with Decimal Format is (2 places) " + withDecimalFormatPattern(F, 2)); - System.out.println(F + " with Decimal Format is (3 places) " + withDecimalFormatPattern(F, 3)); - } - public static double withBigDecimal(double value, int places) { if (places < 0) throw new IllegalArgumentException(); diff --git a/java-numbers/src/test/java/com/baeldung/formatNumber/FormatNumberTest.java b/java-numbers/src/test/java/com/baeldung/formatNumber/FormatNumberTest.java index 1e402e4dc9..cb4e7631f7 100644 --- a/java-numbers/src/test/java/com/baeldung/formatNumber/FormatNumberTest.java +++ b/java-numbers/src/test/java/com/baeldung/formatNumber/FormatNumberTest.java @@ -1,42 +1,37 @@ package com.baeldung.formatNumber; -import org.apache.commons.math3.util.Precision; -import org.decimal4j.util.DoubleRounder; -import org.junit.Assert; import org.junit.Test; +import static com.baeldung.formatNumber.FormatNumber.*; +import static org.assertj.core.api.Assertions.*; + public class FormatNumberTest { - private double value = 2.03456d; - private int places = 2; - private double expected = 2.03d; + private static final double D = 4.2352989244d; + private static final double F = 8.6994540927d; @Test public void givenDecimalNumber_whenFormatNumberToNDecimalPlaces_thenGetExpectedResult() { - double delta = 0.0d; - Assert.assertEquals(expected, FormatNumber.withBigDecimal(value, places), delta); - Assert.assertEquals(expected, FormatNumber.withDecimalFormatPattern(value, places), delta); - Assert.assertEquals(expected, FormatNumber.withMathRound(value, places), delta); - places = 3; - expected = 2.035d; + assertThat(withBigDecimal(D, 2)).isEqualTo(4.24); + assertThat(withBigDecimal(D, 3)).isEqualTo(4.235); + assertThat(withBigDecimal(F, 2)).isEqualTo(8.7); + assertThat(withBigDecimal(F, 3)).isEqualTo(8.699); - Assert.assertEquals(expected, FormatNumber.withBigDecimal(value, places), delta); - Assert.assertEquals(expected, FormatNumber.withDecimalFormatPattern(value, places), delta); - Assert.assertEquals(expected, FormatNumber.withMathRound(value, places), delta); + assertThat(withMathRound(D, 2)).isEqualTo(4.24); + assertThat(withMathRound(D, 3)).isEqualTo(4.235); + assertThat(withMathRound(F, 2)).isEqualTo(8.7); + assertThat(withMathRound(F, 3)).isEqualTo(8.699); - value = 256.024d; - places = 2; - expected = 256.02d; + assertThat(withStringFormat(D, 2)).isEqualTo("4.24"); + assertThat(withStringFormat(D, 3)).isEqualTo("4.235"); + assertThat(withStringFormat(F, 2)).isEqualTo("8.70"); + assertThat(withStringFormat(F, 3)).isEqualTo("8.699"); - Assert.assertEquals(expected, FormatNumber.withBigDecimal(value, places), delta); - Assert.assertEquals(expected, FormatNumber.withDecimalFormatPattern(value, places), delta); - Assert.assertEquals(expected, FormatNumber.withMathRound(value, places), delta); + assertThat(withDecimalFormatLocal(D)).isEqualTo(4.235); + assertThat(withDecimalFormatLocal(F)).isEqualTo(8.699); - value = 260.773d; - places = 2; - expected = 260.77d; - - Assert.assertEquals(expected, FormatNumber.withBigDecimal(value, places), delta); - Assert.assertEquals(expected, FormatNumber.withDecimalFormatPattern(value, places), delta); - Assert.assertEquals(expected, FormatNumber.withMathRound(value, places), delta); + assertThat(withDecimalFormatPattern(D, 2)).isEqualTo(4.24); + assertThat(withDecimalFormatPattern(D, 3)).isEqualTo(4.235); + assertThat(withDecimalFormatPattern(F, 2)).isEqualTo(8.7); + assertThat(withDecimalFormatPattern(F, 3)).isEqualTo(8.699); } }