testing examples for hamcrest

This commit is contained in:
Eugen Paraschiv 2013-10-28 15:30:07 +02:00
parent 31de9bbf62
commit e2371cec01
3 changed files with 70 additions and 2 deletions

View File

@ -1,4 +1,4 @@
package org.baeldung.guava.collections; package org.baeldung.guava;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;

View File

@ -1,4 +1,4 @@
package org.baeldung.guava.collections; package org.baeldung.guava;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;

View File

@ -0,0 +1,68 @@
package org.baeldung.hamcrest;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class HamcrestExamplesTest {
// tests
@Test
public final void whenVerifyingSingleElementIsPartOfCollection_thenCorrect() {
final List<String> collection = Lists.newArrayList("ab", "cd", "ef");
assertThat(collection, hasItem("cd"));
assertThat(collection, not(hasItem("zz")));
}
@Test
public final void whenVerifyingMultipleElementsArePartOfCollection_thenCorrect1() {
final List<String> collection = Lists.newArrayList("ab", "cd", "ef");
assertThat(collection, hasItems("cd", "ef"));
}
@Test
public final void whenVerifyingMultipleElementsArePartOfCollection_thenCorrect2() {
final List<String> collection = Lists.newArrayList("ab", "cd", "ef");
assertThat(collection, contains("ab", "cd", "ef"));
}
@Test
public final void givenCollectionIsEmpty_whenChecking_thenEmpty() {
final List<String> collection = Lists.newArrayList();
assertThat(collection, empty());
}
@Test
public final void givenCollectionIsNotEmpty_whenChecking_thenNotEmpty() {
final List<String> collection = Lists.newArrayList("a");
assertThat(collection, not(empty()));
}
@Test
public final void givenMapIsEmpty_whenChecking_thenEmpty() {
final Map<String, String> collection = Maps.newHashMap();
assertThat(collection, equalTo(Collections.EMPTY_MAP));
}
@Test
public final void givenArrayIsEmpty_whenChecking_thenEmpty() {
final String[] array = new String[] { "ab" };
assertThat(array, not(emptyArray()));
}
}