From 2e1949b898898c5664057f6ca1b1024091caa9b5 Mon Sep 17 00:00:00 2001 From: Magdalena Krause Date: Tue, 3 Apr 2018 18:41:09 -0300 Subject: [PATCH] BAEL-1579: Renaming custom matcher and handling corner case. (#3928) --- .../hamcrest/custommatchers/IsDivisibleBy.java | 1 + .../{IsOnlyNumbers.java => IsOnlyDigits.java} | 8 ++++---- .../baeldung/hamcrest/HamcrestCustomUnitTest.java | 14 +++++++------- 3 files changed, 12 insertions(+), 11 deletions(-) rename testing-modules/mockito/src/main/java/org/baeldung/hamcrest/custommatchers/{IsOnlyNumbers.java => IsOnlyDigits.java} (69%) diff --git a/testing-modules/mockito/src/main/java/org/baeldung/hamcrest/custommatchers/IsDivisibleBy.java b/testing-modules/mockito/src/main/java/org/baeldung/hamcrest/custommatchers/IsDivisibleBy.java index 65a183f273..ea12ce0006 100644 --- a/testing-modules/mockito/src/main/java/org/baeldung/hamcrest/custommatchers/IsDivisibleBy.java +++ b/testing-modules/mockito/src/main/java/org/baeldung/hamcrest/custommatchers/IsDivisibleBy.java @@ -14,6 +14,7 @@ public class IsDivisibleBy extends TypeSafeMatcher { @Override protected boolean matchesSafely(Integer dividend) { + if (divider == 0) return false; return ((dividend % divider) == 0); } diff --git a/testing-modules/mockito/src/main/java/org/baeldung/hamcrest/custommatchers/IsOnlyNumbers.java b/testing-modules/mockito/src/main/java/org/baeldung/hamcrest/custommatchers/IsOnlyDigits.java similarity index 69% rename from testing-modules/mockito/src/main/java/org/baeldung/hamcrest/custommatchers/IsOnlyNumbers.java rename to testing-modules/mockito/src/main/java/org/baeldung/hamcrest/custommatchers/IsOnlyDigits.java index 7d95fbf34a..7515dfbf6d 100644 --- a/testing-modules/mockito/src/main/java/org/baeldung/hamcrest/custommatchers/IsOnlyNumbers.java +++ b/testing-modules/mockito/src/main/java/org/baeldung/hamcrest/custommatchers/IsOnlyDigits.java @@ -4,7 +4,7 @@ import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; -public class IsOnlyNumbers extends TypeSafeMatcher { +public class IsOnlyDigits extends TypeSafeMatcher { @Override protected boolean matchesSafely(String s) { @@ -18,10 +18,10 @@ public class IsOnlyNumbers extends TypeSafeMatcher { @Override public void describeTo(Description description) { - description.appendText("only numbers"); + description.appendText("only digits"); } - public static Matcher onlyNumbers() { - return new IsOnlyNumbers(); + public static Matcher onlyDigits() { + return new IsOnlyDigits(); } } \ No newline at end of file diff --git a/testing-modules/mockito/src/test/java/org/baeldung/hamcrest/HamcrestCustomUnitTest.java b/testing-modules/mockito/src/test/java/org/baeldung/hamcrest/HamcrestCustomUnitTest.java index 4d6ff8503f..6cc8692bc7 100644 --- a/testing-modules/mockito/src/test/java/org/baeldung/hamcrest/HamcrestCustomUnitTest.java +++ b/testing-modules/mockito/src/test/java/org/baeldung/hamcrest/HamcrestCustomUnitTest.java @@ -3,7 +3,7 @@ package org.baeldung.hamcrest; import org.junit.Test; import static org.baeldung.hamcrest.custommatchers.IsDivisibleBy.divisibleBy; -import static org.baeldung.hamcrest.custommatchers.IsOnlyNumbers.onlyNumbers; +import static org.baeldung.hamcrest.custommatchers.IsOnlyDigits.onlyDigits; import static org.baeldung.hamcrest.custommatchers.IsUppercase.uppercase; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; @@ -12,17 +12,17 @@ import static org.hamcrest.Matchers.not; public class HamcrestCustomUnitTest { @Test - public final void givenAString_whenIsOnlyNumbers_thenCorrect() { - String numbers = "123"; + public final void givenAString_whenIsOnlyDigits_thenCorrect() { + String digits = "123"; - assertThat(numbers, is(onlyNumbers())); + assertThat(digits, is(onlyDigits())); } @Test - public final void givenAString_whenIsNotOnlyNumbers_thenCorrect() { - String numbers = "123ABC"; + public final void givenAString_whenIsNotOnlyDigits_thenCorrect() { + String aphanumeric = "123ABC"; - assertThat(numbers, is(not(onlyNumbers()))); + assertThat(aphanumeric, is(not(onlyDigits()))); } @Test