Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert to check string contains only numeric values

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,

like image 390
Romi Avatar asked Aug 01 '26 13:08

Romi


1 Answers

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();
}
like image 177
MauroB Avatar answered Aug 04 '26 02:08

MauroB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!