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
@@ -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))));
}
}