string to long primitive or long object unit tests

This commit is contained in:
danielmcnally285 2023-12-29 11:54:15 +00:00
parent 3eb54511ed
commit 4ba7ed5d80
1 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,89 @@
package com.baeldung.stringtolong;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.commons.lang3.math.NumberUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import com.google.common.primitives.Longs;
public class StringToLongPrimitiveOrLongObjectUnitTest {
@Test
public void givenString_whenUsingLongConstructor_thenObtainLongObject() {
Long l = new Long("123456");
assertThat(l).isEqualTo(123456L);
}
@Test
public void givenInvalidString_whenUsingLongConstructor_thenNumberFormatExceptionThrown() {
Assertions.assertThrows(NumberFormatException.class, () -> new Long("Invalid String"));
}
@Test
public void givenString_whenUsingLongValueOf_thenObtainLongObject() {
Long l = Long.valueOf("123456");
assertThat(l).isEqualTo(123456L);
}
@Test
public void givenInvalidString_whenUsingLongValueOf_thenNumberFormatExceptionThrown() {
Assertions.assertThrows(NumberFormatException.class, () -> Long.valueOf("Invalid String"));
}
@Test
public void givenString_whenUsingParseLong_thenObtainLongPrimitive() {
long l = Long.parseLong("123456");
assertThat(l).isEqualTo(123456L);
}
@Test
public void givenInvalidString_whenUsingParseLong_thenNumberFormatExceptionThrown() {
Assertions.assertThrows(NumberFormatException.class, () -> Long.parseLong("Invalid String"));
}
@Test
public void givenHexadecimalString_whenUsingLongDecode_thenObtainLongObject() {
Long l = Long.decode("0x1e240");
assertThat(l).isEqualTo(123456L);
}
@Test
public void givenInvalidString_whenUsingLongDecode_thenNumberFormatExceptionThrown() {
Assertions.assertThrows(NumberFormatException.class, () -> Long.decode("Invalid String"));
}
@Test
public void givenHexadecimalString_whenUsingApacheCommons_thenObtainLongObject() {
Long l = NumberUtils.createLong("0x1e240");
assertThat(l).isEqualTo(123456L);
}
@Test
public void givenInvalidString_whenUsingApacheCommons_thenNumberFormatExceptionThrown() {
Assertions.assertThrows(NumberFormatException.class, () -> NumberUtils.createLong("Invalid String"));
}
@Test
public void givenString_whenUsingGuava_thenObtainLongObject() {
Long l = Longs.tryParse("123456");
assertThat(l).isEqualTo(123456L);
}
@Test
public void givenInvalidString_whenUsingGuava_thenObtainNull() {
assertThat(Longs.tryParse("Invalid String")).isNull();
}
@Test
public void givenString_whenUsingParseUnsignedLong_thenObtainUnsignedLongObject() {
Long l = Long.parseUnsignedLong("9223372036854775808");
assertThat(Long.toUnsignedString(l)).isEqualTo("9223372036854775808");
}
@Test
public void givenInvalidString_whenUsingParseUnsignedLong_thenNumberFormatExceptionThrown() {
Assertions.assertThrows(NumberFormatException.class, () -> Long.parseUnsignedLong("Invalid String"));
}
}