BALE-2154 - Fix Junit.

This commit is contained in:
chandra 2018-09-09 13:18:50 -04:00
parent b94147540a
commit 2b67a1b48b
3 changed files with 27 additions and 29 deletions

View File

@ -1,3 +1,5 @@
package com.baeldung.maths;
import java.math.BigDecimal;
import java.math.RoundingMode;
@ -11,16 +13,16 @@ public class BigDecimalDemo {
* @return
*/
public static BigDecimal calculateTotalAmount(BigDecimal quantity,
BigDecimal unitPrice, BigDecimal discountRate, BigDecimal taxRate) {
BigDecimal unitPrice, BigDecimal discountRate, BigDecimal taxRate) {
BigDecimal amount = quantity.multiply(unitPrice);
BigDecimal discount = amount.multiply(discountRate);
BigDecimal discountedAmount = amount.subtract(discount);
BigDecimal tax = taxRate.multiply(taxRate);
BigDecimal tax = discountedAmount.multiply(taxRate);
BigDecimal total = discountedAmount.add(tax);
// round to 2 decimal places using HALF_EVEN
BigDecimal roundedTotal = total.setScale(2, RoundingMode.HALF_EVEN);
return roundedTotal;
}

View File

@ -1,3 +1,5 @@
package com.baeldung.maths;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
@ -14,14 +16,7 @@ import org.junit.jupiter.api.Test;
public class BigDecimalDemoUnitTest {
@Test
void test() {
System.out.println(BigDecimal.ONE
.divide(BigDecimal.valueOf(3), 4, RoundingMode.CEILING)
.precision());
}
@Test
void whenBigDecimalCreated_thenValueMatches() {
public void whenBigDecimalCreated_thenValueMatches() {
BigDecimal bdFromString = new BigDecimal("0.1");
BigDecimal bdFromCharArray = new BigDecimal(
new char[] { '3', '.', '1', '6', '1', '5' });
@ -38,13 +33,13 @@ public class BigDecimalDemoUnitTest {
}
@Test
void whenBigDecimalCreatedFromDouble_thenValueMayNotMatch() {
public void whenBigDecimalCreatedFromDouble_thenValueMayNotMatch() {
BigDecimal bdFromDouble = new BigDecimal(0.1d);
assertNotEquals("0.1", bdFromDouble.toString());
}
@Test
void whenBigDecimalCreatedUsingValueOf_thenValueMatches() {
public void whenBigDecimalCreatedUsingValueOf_thenValueMatches() {
BigDecimal bdFromLong1 = BigDecimal.valueOf(123412345678901L);
BigDecimal bdFromLong2 = BigDecimal.valueOf(123412345678901L, 2);
BigDecimal bdFromDouble = BigDecimal.valueOf(0.1d);
@ -55,7 +50,7 @@ public class BigDecimalDemoUnitTest {
}
@Test
void whenEqualsCalled_thenSizeAndScaleMatched() {
public void whenEqualsCalled_thenSizeAndScaleMatched() {
BigDecimal bd1 = new BigDecimal("1.0");
BigDecimal bd2 = new BigDecimal("1.00");
@ -63,7 +58,7 @@ public class BigDecimalDemoUnitTest {
}
@Test
void whenComparingBigDecimals_thenExpectedResult() {
public void whenComparingBigDecimals_thenExpectedResult() {
BigDecimal bd1 = new BigDecimal("1.0");
BigDecimal bd2 = new BigDecimal("1.00");
BigDecimal bd3 = new BigDecimal("2.0");
@ -77,7 +72,7 @@ public class BigDecimalDemoUnitTest {
}
@Test
void whenPerformingArithmetic_thenExpectedResult() {
public void whenPerformingArithmetic_thenExpectedResult() {
BigDecimal bd1 = new BigDecimal("4.0");
BigDecimal bd2 = new BigDecimal("2.0");
@ -93,7 +88,7 @@ public class BigDecimalDemoUnitTest {
}
@Test
void whenGettingAttributes_thenExpectedResult() {
public void whenGettingAttributes_thenExpectedResult() {
BigDecimal bd = new BigDecimal("-12345.6789");
assertEquals(9, bd.precision());
@ -102,7 +97,7 @@ public class BigDecimalDemoUnitTest {
}
@Test
void whenRoundingDecimal_thenExpectedResult() {
public void whenRoundingDecimal_thenExpectedResult() {
BigDecimal bd = new BigDecimal("2.5");
// Round to 1 digit using HALF_EVEN
BigDecimal rounded = bd
@ -112,7 +107,7 @@ public class BigDecimalDemoUnitTest {
}
@Test
void givenPurchaseTxn_whenCalculatingTotalAmount_thenExpectedResult() {
public void givenPurchaseTxn_whenCalculatingTotalAmount_thenExpectedResult() {
BigDecimal quantity = new BigDecimal("4.5");
BigDecimal unitPrice = new BigDecimal("2.69");
BigDecimal discountRate = new BigDecimal("0.10");
@ -120,7 +115,6 @@ public class BigDecimalDemoUnitTest {
BigDecimal amountToBePaid = BigDecimalDemo
.calculateTotalAmount(quantity, unitPrice, discountRate, taxRate);
assertEquals("10.90", amountToBePaid.toString());
assertEquals("11.68", amountToBePaid.toString());
}
}

View File

@ -1,3 +1,5 @@
package com.baeldung.maths;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@ -9,7 +11,7 @@ import org.junit.jupiter.api.Test;
public class BigIntegerDemoUnitTest {
@Test
void whenBigIntegerCreatedFromConstructor_thenExpectedResult() {
public void whenBigIntegerCreatedFromConstructor_thenExpectedResult() {
BigInteger biFromString = new BigInteger("1234567890987654321");
BigInteger biFromByteArray = new BigInteger(
new byte[] { 64, 64, 64, 64, 64, 64 });
@ -22,14 +24,14 @@ public class BigIntegerDemoUnitTest {
}
@Test
void whenLongConvertedToBigInteger_thenValueMatches() {
public void whenLongConvertedToBigInteger_thenValueMatches() {
BigInteger bi = BigInteger.valueOf(2305843009213693951L);
assertEquals("2305843009213693951", bi.toString());
}
@Test
void givenBigIntegers_whentCompared_thenExpectedResult() {
public void givenBigIntegers_whentCompared_thenExpectedResult() {
BigInteger i = new BigInteger("123456789012345678901234567890");
BigInteger j = new BigInteger("123456789012345678901234567891");
BigInteger k = new BigInteger("123456789012345678901234567892");
@ -40,7 +42,7 @@ public class BigIntegerDemoUnitTest {
}
@Test
void givenBigIntegers_whenPerformingArithmetic_thenExpectedResult() {
public void givenBigIntegers_whenPerformingArithmetic_thenExpectedResult() {
BigInteger i = new BigInteger("4");
BigInteger j = new BigInteger("2");
@ -56,7 +58,7 @@ public class BigIntegerDemoUnitTest {
}
@Test
void givenBigIntegers_whenPerformingBitOperations_thenExpectedResult() {
public void givenBigIntegers_whenPerformingBitOperations_thenExpectedResult() {
BigInteger i = new BigInteger("17");
BigInteger j = new BigInteger("7");
@ -78,7 +80,7 @@ public class BigIntegerDemoUnitTest {
}
@Test
void givenBigIntegers_whenPerformingBitManipulations_thenExpectedResult() {
public void givenBigIntegers_whenPerformingBitManipulations_thenExpectedResult() {
BigInteger i = new BigInteger("1018");
int bitCount = i.bitCount();
@ -99,7 +101,7 @@ public class BigIntegerDemoUnitTest {
}
@Test
void givenBigIntegers_whenModularCalculation_thenExpectedResult() {
public void givenBigIntegers_whenModularCalculation_thenExpectedResult() {
BigInteger i = new BigInteger("31");
BigInteger j = new BigInteger("24");
BigInteger k = new BigInteger("16");
@ -117,7 +119,7 @@ public class BigIntegerDemoUnitTest {
}
@Test
void givenBigIntegers_whenPrimeOperations_thenExpectedResult() {
public void givenBigIntegers_whenPrimeOperations_thenExpectedResult() {
BigInteger i = BigInteger.probablePrime(100, new Random());
boolean isProbablePrime = i.isProbablePrime(1000);