BAEL-1579: Hamcrest custom matchers. (#3905)

* BAEL-1579: Hamcrest custom matchers.

* BAEL-1589: Removing hamcrest dependency test scope.
This commit is contained in:
Magdalena Krause 2018-03-29 17:58:19 -03:00 committed by maibin
parent 48b8d2c8a5
commit 6549e41afa
5 changed files with 127 additions and 1 deletions

View File

@ -51,7 +51,6 @@
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -0,0 +1,28 @@
package org.baeldung.hamcrest.custommatchers;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
public class IsDivisibleBy extends TypeSafeMatcher<Integer> {
private Integer divider;
private IsDivisibleBy(Integer divider) {
this.divider = divider;
}
@Override
protected boolean matchesSafely(Integer dividend) {
return ((dividend % divider) == 0);
}
@Override
public void describeTo(Description description) {
description.appendText("divisible by " + divider);
}
public static Matcher<Integer> divisibleBy(Integer divider) {
return new IsDivisibleBy(divider);
}
}

View File

@ -0,0 +1,27 @@
package org.baeldung.hamcrest.custommatchers;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
public class IsOnlyNumbers extends TypeSafeMatcher<String> {
@Override
protected boolean matchesSafely(String s) {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
@Override
public void describeTo(Description description) {
description.appendText("only numbers");
}
public static Matcher<String> onlyNumbers() {
return new IsOnlyNumbers();
}
}

View File

@ -0,0 +1,22 @@
package org.baeldung.hamcrest.custommatchers;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
public class IsUppercase extends TypeSafeMatcher<String> {
@Override
protected boolean matchesSafely(String s) {
return s.equals(s.toUpperCase());
}
@Override
public void describeTo(Description description) {
description.appendText("all uppercase");
}
public static Matcher<String> uppercase() {
return new IsUppercase();
}
}

View File

@ -0,0 +1,50 @@
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.IsUppercase.uppercase;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
public class HamcrestCustomUnitTest {
@Test
public final void givenAString_whenIsOnlyNumbers_thenCorrect() {
String numbers = "123";
assertThat(numbers, is(onlyNumbers()));
}
@Test
public final void givenAString_whenIsNotOnlyNumbers_thenCorrect() {
String numbers = "123ABC";
assertThat(numbers, is(not(onlyNumbers())));
}
@Test
public final void givenAString_whenIsUppercase_thenCorrect() {
String uppercaseWord = "HELLO";
assertThat(uppercaseWord, is(uppercase()));
}
@Test
public final void givenAnEvenInteger_whenDivisibleByTwo_thenCorrect() {
Integer ten = 10;
Integer two = 2;
assertThat(ten, is(divisibleBy(two)));
}
@Test
public final void givenAnOddInteger_whenNotDivisibleByTwo_thenCorrect() {
Integer eleven = 11;
Integer two = 2;
assertThat(eleven, is(not(divisibleBy(two))));
}
}