BAEL-3141: Added test cases for String to Double conversion. (#7448)
These tests exercise the conversion of Strings that can be converted to Doubles, as well as the exceptions that are thrown for invalid Strings.
This commit is contained in:
parent
1baace10cf
commit
db90a53e38
|
@ -0,0 +1,56 @@
|
|||
package com.baeldung.string.todouble;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.ParseException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringToDoubleConversionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidString_WhenParseDouble_ThenResultIsPrimitiveDouble() {
|
||||
assertEquals(1.23, Double.parseDouble("1.23"), 0.000001);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullString_WhenParseDouble_ThenNullPointerExceptionIsThrown() {
|
||||
Double.parseDouble(null);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void givenInalidString_WhenParseDouble_ThenNumberFormatExceptionIsThrown() {
|
||||
Double.parseDouble("&");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidString_WhenValueOf_ThenResultIsPrimitiveDouble() {
|
||||
assertEquals(1.23, Double.valueOf("1.23"), 0.000001);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullString_WhenValueOf_ThenNullPointerExceptionIsThrown() {
|
||||
Double.valueOf(null);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void givenInalidString_WhenValueOf_ThenNumberFormatExceptionIsThrown() {
|
||||
Double.valueOf("&");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidString_WhenDecimalFormat_ThenResultIsValidDouble() throws ParseException {
|
||||
assertEquals(1.23, new DecimalFormat("#").parse("1.23").doubleValue(), 0.000001);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullString_WhenDecimalFormat_ThenNullPointerExceptionIsThrown() throws ParseException {
|
||||
new DecimalFormat("#").parse(null);
|
||||
}
|
||||
|
||||
@Test(expected = ParseException.class)
|
||||
public void givenInvalidString_WhenDecimalFormat_ThenParseExceptionIsThrown() throws ParseException {
|
||||
new DecimalFormat("#").parse("&");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue