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
protected boolean matchesSafely(Integer dividend) {
if (divider == 0) return false;
return ((dividend % divider) == 0);
}

View File

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

View File

@ -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