BAEL-1579: Renaming custom matcher and handling corner case. (#3928)

This commit is contained in:
Magdalena Krause 2018-04-03 18:41:09 -03:00 committed by maibin
parent ad40040852
commit 2e1949b898
3 changed files with 12 additions and 11 deletions

View File

@ -14,6 +14,7 @@ public class IsDivisibleBy extends TypeSafeMatcher<Integer> {
@Override @Override
protected boolean matchesSafely(Integer dividend) { protected boolean matchesSafely(Integer dividend) {
if (divider == 0) return false;
return ((dividend % divider) == 0); return ((dividend % divider) == 0);
} }

View File

@ -4,7 +4,7 @@ import org.hamcrest.Description;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher; import org.hamcrest.TypeSafeMatcher;
public class IsOnlyNumbers extends TypeSafeMatcher<String> { public class IsOnlyDigits extends TypeSafeMatcher<String> {
@Override @Override
protected boolean matchesSafely(String s) { protected boolean matchesSafely(String s) {
@ -18,10 +18,10 @@ public class IsOnlyNumbers extends TypeSafeMatcher<String> {
@Override @Override
public void describeTo(Description description) { public void describeTo(Description description) {
description.appendText("only numbers"); description.appendText("only digits");
} }
public static Matcher<String> onlyNumbers() { public static Matcher<String> onlyDigits() {
return new IsOnlyNumbers(); return new IsOnlyDigits();
} }
} }

View File

@ -3,7 +3,7 @@ package org.baeldung.hamcrest;
import org.junit.Test; import org.junit.Test;
import static org.baeldung.hamcrest.custommatchers.IsDivisibleBy.divisibleBy; 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.baeldung.hamcrest.custommatchers.IsUppercase.uppercase;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
@ -12,17 +12,17 @@ import static org.hamcrest.Matchers.not;
public class HamcrestCustomUnitTest { public class HamcrestCustomUnitTest {
@Test @Test
public final void givenAString_whenIsOnlyNumbers_thenCorrect() { public final void givenAString_whenIsOnlyDigits_thenCorrect() {
String numbers = "123"; String digits = "123";
assertThat(numbers, is(onlyNumbers())); assertThat(digits, is(onlyDigits()));
} }
@Test @Test
public final void givenAString_whenIsNotOnlyNumbers_thenCorrect() { public final void givenAString_whenIsNotOnlyDigits_thenCorrect() {
String numbers = "123ABC"; String aphanumeric = "123ABC";
assertThat(numbers, is(not(onlyNumbers()))); assertThat(aphanumeric, is(not(onlyDigits())));
} }
@Test @Test