I want to write a JUNIT test case for checking that a String contains only numeric values. Can anybody suggest me to do so. I am new to junit test case and i cud not find any way to write an assert for it. Please suggest.
Thanks,
If you use AssertJ, you have the method "containsOnlyDigits"
@Test
void shouldContainOnlyDigits() {
assertThat("123").containsOnlyDigits();
}
or you can improve the check if you have a String that should contain a padded number:
@Test
void shouldContainOnlyDigits() {
assertThat("001").hasSize(3).containsOnlyDigits();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With