testing work for hamcrest

This commit is contained in:
Eugen Paraschiv 2013-10-28 16:56:35 +02:00
parent e2371cec01
commit 3d0def73d4
1 changed files with 31 additions and 2 deletions

View File

@ -1,11 +1,15 @@
package org.baeldung.hamcrest;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
@ -13,6 +17,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.hamcrest.Matchers;
import org.junit.Test;
import com.google.common.collect.Lists;
@ -32,15 +37,21 @@ public class HamcrestExamplesTest {
@Test
public final void whenVerifyingMultipleElementsArePartOfCollection_thenCorrect1() {
final List<String> collection = Lists.newArrayList("ab", "cd", "ef");
assertThat(collection, hasItems("cd", "ef"));
assertThat(collection, hasItems("ef", "cd"));
}
@Test
public final void whenVerifyingMultipleElementsArePartOfCollection_thenCorrect2() {
public final void whenVerifyingMultipleElementsArePartOfCollectionInStrictOrder_thenCorrect2() {
final List<String> collection = Lists.newArrayList("ab", "cd", "ef");
assertThat(collection, contains("ab", "cd", "ef"));
}
@Test
public final void whenVerifyingMultipleElementsArePartOfCollectionInAnyOrder_thenCorrect2() {
final List<String> collection = Lists.newArrayList("ab", "cd", "ef");
assertThat(collection, containsInAnyOrder("cd", "ab", "ef"));
}
@Test
public final void givenCollectionIsEmpty_whenChecking_thenEmpty() {
final List<String> collection = Lists.newArrayList();
@ -65,4 +76,22 @@ public class HamcrestExamplesTest {
assertThat(array, not(emptyArray()));
}
@Test
public final void whenCollectionSizeIsChecked_thenCorrect() {
final List<String> collection = Lists.newArrayList("ab", "cd", "ef");
assertThat(collection, hasSize(3));
}
@Test
public final void whenIterableSizeIsChecked_thenCorrect() {
final Iterable<String> collection = Lists.newArrayList("ab", "cd", "ef");
assertThat(collection, Matchers.<String> iterableWithSize(3));
}
@Test
public final void whenCheckingConditionOverEachItem_thenCorrect() {
final List<Integer> collection = Lists.newArrayList(15, 20, 25, 30);
assertThat(collection, everyItem(greaterThan(10)));
}
}