Hamcrest Assertion code example

Example: Hamcrest Assertion

Hamcrest Assertion Library: 
import static org.hamcrest.Matchers.*;
RestAssuerd Library use Hamcrest matchers for its 
assertions in most cases. Hamcrest Library is included in 
RestAssuerd dependency already. we do not need to add extra 
dependency in pom file
Few common matchers I used :
is(.. )
not(..)
equalTo(..)
containString(...)
greaterThan(..)
lessThan(..)
hasSize(..)
hasItem(..) and hasItems(...)

import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class BiscuitTest {
@Test
public void testEquals() {
Biscuit theBiscuit = new Biscuit("Ginger");
Biscuit myBiscuit = new Biscuit("Ginger");
assertThat(theBiscuit, equalTo(myBiscuit));

Tags:

Misc Example