BAEL-7447: Integer to BigDecimal (#15563)

This commit is contained in:
Eugene Kovko 2024-01-08 03:50:22 +01:00 committed by GitHub
parent bf3b5a42f8
commit 36edee6be7
2 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,33 @@
package com.baeldung.integertobigdecimal;
import java.math.BigDecimal;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
public class BigDecimalConversionArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of(Integer.valueOf(0), BigDecimal.valueOf(0)),
Arguments.of(Integer.valueOf(1), BigDecimal.valueOf(1)),
Arguments.of(Integer.valueOf(2), BigDecimal.valueOf(2)),
Arguments.of(Integer.valueOf(3), BigDecimal.valueOf(3)),
Arguments.of(Integer.valueOf(4), BigDecimal.valueOf(4)),
Arguments.of(Integer.valueOf(5), BigDecimal.valueOf(5)),
Arguments.of(Integer.valueOf(6), BigDecimal.valueOf(6)),
Arguments.of(Integer.valueOf(7), BigDecimal.valueOf(7)),
Arguments.of(Integer.valueOf(8), BigDecimal.valueOf(8)),
Arguments.of(Integer.valueOf(9), BigDecimal.valueOf(9)),
Arguments.of(Integer.valueOf(10), BigDecimal.valueOf(10)),
Arguments.of(Integer.valueOf(11), BigDecimal.valueOf(11)),
Arguments.of(Integer.valueOf(12), BigDecimal.valueOf(12)),
Arguments.of(Integer.valueOf(13), BigDecimal.valueOf(13)),
Arguments.of(Integer.valueOf(14), BigDecimal.valueOf(14)),
Arguments.of(Integer.valueOf(15), BigDecimal.valueOf(15))
);
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.integertobigdecimal;
import static org.assertj.core.api.Assertions.assertThat;
import java.math.BigDecimal;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.ValueSource;
class IntegerToBigDecimalUnitTest {
@ParameterizedTest
@ArgumentsSource(BigDecimalConversionArgumentsProvider.class)
void giveIntegerWhenConvertWithConstructorToBigDecimalThenConversionCorrect(Integer given, BigDecimal expected) {
BigDecimal actual = new BigDecimal(given);
assertThat(actual).isEqualTo(expected);
}
@ParameterizedTest
@ValueSource(ints = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
void giveIntegerWhenConvertWithConstructorToBigDecimalThenConversionWithoutCaching(Integer given) {
BigDecimal firstBigDecimal = new BigDecimal(given);
BigDecimal secondBigDecimal = new BigDecimal(given);
assertThat(firstBigDecimal)
.isEqualTo(secondBigDecimal)
.isNotSameAs(secondBigDecimal);
}
@ParameterizedTest
@ArgumentsSource(BigDecimalConversionArgumentsProvider.class)
void giveIntegerWhenConvertWithValueOfToBigDecimalThenConversionCorrect(Integer given, BigDecimal expected) {
BigDecimal actual = BigDecimal.valueOf(given);
assertThat(actual).isEqualTo(expected);
}
@ParameterizedTest
@ValueSource(ints = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
void giveIntegerWhenConvertWithValueOfToBigDecimalThenConversionCachesTheResults(Integer given) {
BigDecimal firstBigDecimal = BigDecimal.valueOf(given);
BigDecimal secondBigDecimal = BigDecimal.valueOf(given);
assertThat(firstBigDecimal).isSameAs(secondBigDecimal);
}
@ParameterizedTest
@ValueSource(ints = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21})
void giveIntegerWhenConvertWithValueOfToBigDecimalThenConversionWontCacheTheResults(Integer given) {
BigDecimal firstBigDecimal = BigDecimal.valueOf(given);
BigDecimal secondBigDecimal = BigDecimal.valueOf(given);
assertThat(firstBigDecimal)
.isEqualTo(secondBigDecimal)
.isNotSameAs(secondBigDecimal);
}
}