File tree Expand file tree Collapse file tree
testing-modules/mockito/src
main/java/org/baeldung/hamcrest/custommatchers
test/java/org/baeldung/hamcrest Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ private IsDivisibleBy(Integer divider) {
1414
1515 @ Override
1616 protected boolean matchesSafely (Integer dividend ) {
17+ if (divider == 0 ) return false ;
1718 return ((dividend % divider ) == 0 );
1819 }
1920
Original file line number Diff line number Diff line change 44import org .hamcrest .Matcher ;
55import org .hamcrest .TypeSafeMatcher ;
66
7- public class IsOnlyNumbers extends TypeSafeMatcher <String > {
7+ public class IsOnlyDigits extends TypeSafeMatcher <String > {
88
99 @ Override
1010 protected boolean matchesSafely (String s ) {
@@ -18,10 +18,10 @@ protected boolean matchesSafely(String s) {
1818
1919 @ Override
2020 public void describeTo (Description description ) {
21- description .appendText ("only numbers " );
21+ description .appendText ("only digits " );
2222 }
2323
24- public static Matcher <String > onlyNumbers () {
25- return new IsOnlyNumbers ();
24+ public static Matcher <String > onlyDigits () {
25+ return new IsOnlyDigits ();
2626 }
2727}
Original file line number Diff line number Diff line change 33import org .junit .Test ;
44
55import static org .baeldung .hamcrest .custommatchers .IsDivisibleBy .divisibleBy ;
6- import static org .baeldung .hamcrest .custommatchers .IsOnlyNumbers . onlyNumbers ;
6+ import static org .baeldung .hamcrest .custommatchers .IsOnlyDigits . onlyDigits ;
77import static org .baeldung .hamcrest .custommatchers .IsUppercase .uppercase ;
88import static org .hamcrest .MatcherAssert .assertThat ;
99import static org .hamcrest .Matchers .is ;
1212public class HamcrestCustomUnitTest {
1313
1414 @ Test
15- public final void givenAString_whenIsOnlyNumbers_thenCorrect () {
16- String numbers = "123" ;
15+ public final void givenAString_whenIsOnlyDigits_thenCorrect () {
16+ String digits = "123" ;
1717
18- assertThat (numbers , is (onlyNumbers ()));
18+ assertThat (digits , is (onlyDigits ()));
1919 }
2020
2121 @ Test
22- public final void givenAString_whenIsNotOnlyNumbers_thenCorrect () {
23- String numbers = "123ABC" ;
22+ public final void givenAString_whenIsNotOnlyDigits_thenCorrect () {
23+ String aphanumeric = "123ABC" ;
2424
25- assertThat (numbers , is (not (onlyNumbers ())));
25+ assertThat (aphanumeric , is (not (onlyDigits ())));
2626 }
2727
2828 @ Test
You can’t perform that action at this time.
0 commit comments