Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.junit.Test;
- import org.junit.Before;
- import static org.junit.Assert.*;
- import static org.hamcrest.core.Is.is;
- import static org.hamcrest.core.Is.isA;
- import static org.hamcrest.core.IsNot.not;
- import static org.hamcrest.core.IsEqual.equalTo;
- import static org.hamcrest.core.IsSame.sameInstance;
- import static org.hamcrest.core.AllOf.allOf;
- import static org.hamcrest.core.AnyOf.anyOf;
- import static org.hamcrest.core.StringContains.containsString;
- import static org.hamcrest.core.CombinableMatcher.both;
- import static org.hamcrest.core.CombinableMatcher.either;
- import static org.hamcrest.core.IsInstanceOf.instanceOf;
- import static org.hamcrest.core.IsInstanceOf.any;
- import static org.hamcrest.core.IsCollectionContaining.hasItem;
- import static org.hamcrest.core.IsCollectionContaining.hasItems;
- import static org.hamcrest.core.StringStartsWith.startsWith;
- import static org.hamcrest.core.StringEndsWith.endsWith;
- public class TestsHamcrest {
- @Before
- public void setUp() throws Exception {}
- // Base
- @Test
- public void isExample() {
- assertThat("foo", is("foo"));
- assertThat("foo", equalTo("foo"));
- assertThat("foo", is(equalTo("foo")));
- }
- @Test
- public void notExample() {
- assertThat("foo", is(not("bar")));
- }
- // Objetos
- @Test
- public void equalToExample() {
- assertThat("foo", equalTo("foo"));
- // Distinto objeto pero coincide en longitud e items
- assertThat(new String[] {"foo", "bar"}, is(equalTo(new String[] {"foo", "bar"})));
- assertThat(new String[] {"foo", "bar"}, is(new String[] {"foo", "bar"}));
- // El orden no es el mismo!
- assertThat(new String[] {"bar", "foo"}, is(not(equalTo(new String[] {"foo", "bar"}))));
- }
- @Test
- public void sameInstanceExample() {
- String foo = "foo";
- String sameFoo = foo;
- assertThat(foo, is(sameInstance(sameFoo)));
- }
- @Test
- public void instanceOfExample() {
- assertThat("foo", is(not(instanceOf(Integer.class))));
- assertThat("foo", isA(String.class));
- }
- @Test
- public void anyExample() {
- // No coincide el tipo!
- // assertThat("foo", is(not(any(Integer.class))));
- assertThat("foo", is(any(String.class)));
- }
- // Cadenas
- @Test
- public void startsWithExample() {
- assertThat("foo bar baz", startsWith("foo"));
- }
- @Test
- public void endsWithExample() {
- assertThat("foo bar baz", endsWith("baz"));
- }
- @Test
- public void constainsStringExample() {
- assertThat("foo bar baz", containsString("bar"));
- }
- // Booleanos
- @Test
- public void allOfExample() {
- assertThat("foo bar baz", allOf(startsWith("foo"), endsWith("baz")));
- }
- @Test
- public void anyOfExample() {
- assertThat("foo bar qux", anyOf(startsWith("foo"), endsWith("baz")));
- }
- @Test
- public void bothExample() {
- assertThat("foo bar baz", both(startsWith("foo")).and(endsWith("baz")));
- }
- @Test
- public void eitherExample() {
- assertThat("foo bar baz", either(startsWith("foo")).or(endsWith("baz")));
- }
- // Colecciones
- @Test
- public void hasItemExample() {
- assertThat(java.util.Arrays.asList("foo", "bar", "baz"), hasItem("bar"));
- assertThat(java.util.Arrays.asList("foo", "bar", "baz"), hasItem(startsWith("b")));
- }
- @Test
- public void hasItemsExample() {
- assertThat(java.util.Arrays.asList("foo", "bar", "baz"), hasItems("bar", "baz"));
- assertThat(java.util.Arrays.asList("foo", "bar", "baz"), hasItems(endsWith("o"), endsWith("z")));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment