Add extra test case for MathUtils.cast

This commit is contained in:
Ruben Dijkstra 2016-01-24 22:28:28 +01:00
parent a0f0262357
commit 5dccd2c38b

View File

@ -18,11 +18,17 @@ import static org.junit.Assert.assertSame;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class MathUtilsTest {
@Rule
public final ExpectedException expectedException = ExpectedException.none();
@Test
public void sum() {
assertEquals(Integer.valueOf(5), MathUtils.sum(2, 3.0));
@ -57,6 +63,14 @@ public class MathUtilsTest {
checkSame((byte) 1, Byte.class);
}
@Test
public void cast_throws_on_unsupported_numbers() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Unsupported target type");
checkCast(1, AtomicInteger.class);
}
private static void checkCast(Number value, Class<? extends Number> targetClass) {
Number target = MathUtils.cast(value, targetClass);
assertSame(targetClass, target.getClass());