Advertisement
Guest User

Ejemplo con JUnit y Hamcrest

a guest
Jun 25th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. import org.junit.Test;
  2. import org.junit.Before;
  3. import static org.junit.Assert.*;
  4. import static org.hamcrest.core.Is.is;
  5. import static org.hamcrest.core.Is.isA;
  6. import static org.hamcrest.core.IsNot.not;
  7. import static org.hamcrest.core.IsEqual.equalTo;
  8. import static org.hamcrest.core.IsSame.sameInstance;
  9. import static org.hamcrest.core.AllOf.allOf;
  10. import static org.hamcrest.core.AnyOf.anyOf;
  11. import static org.hamcrest.core.StringContains.containsString;
  12. import static org.hamcrest.core.CombinableMatcher.both;
  13. import static org.hamcrest.core.CombinableMatcher.either;
  14. import static org.hamcrest.core.IsInstanceOf.instanceOf;
  15. import static org.hamcrest.core.IsInstanceOf.any;
  16. import static org.hamcrest.core.IsCollectionContaining.hasItem;
  17. import static org.hamcrest.core.IsCollectionContaining.hasItems;
  18. import static org.hamcrest.core.StringStartsWith.startsWith;
  19. import static org.hamcrest.core.StringEndsWith.endsWith;
  20.  
  21. public class TestsHamcrest {
  22.  
  23.     @Before
  24.     public void setUp() throws Exception {}
  25.  
  26.     // Base
  27.  
  28.     @Test
  29.     public void isExample() {
  30.         assertThat("foo", is("foo"));
  31.         assertThat("foo", equalTo("foo"));
  32.         assertThat("foo", is(equalTo("foo")));
  33.     }
  34.  
  35.     @Test
  36.     public void notExample() {
  37.         assertThat("foo", is(not("bar")));
  38.     }
  39.  
  40.     // Objetos
  41.  
  42.     @Test
  43.     public void equalToExample() {
  44.         assertThat("foo", equalTo("foo"));
  45.         // Distinto objeto pero coincide en longitud e items
  46.         assertThat(new String[] {"foo", "bar"}, is(equalTo(new String[] {"foo", "bar"})));
  47.         assertThat(new String[] {"foo", "bar"}, is(new String[] {"foo", "bar"}));
  48.         // El orden no es el mismo!
  49.         assertThat(new String[] {"bar", "foo"}, is(not(equalTo(new String[] {"foo", "bar"}))));
  50.     }
  51.  
  52.     @Test
  53.     public void sameInstanceExample() {
  54.         String foo = "foo";
  55.         String sameFoo = foo;
  56.         assertThat(foo, is(sameInstance(sameFoo)));
  57.     }
  58.  
  59.     @Test
  60.     public void instanceOfExample() {
  61.         assertThat("foo", is(not(instanceOf(Integer.class))));
  62.         assertThat("foo", isA(String.class));
  63.     }
  64.  
  65.     @Test
  66.     public void anyExample() {
  67.         // No coincide el tipo!
  68.         // assertThat("foo", is(not(any(Integer.class))));
  69.         assertThat("foo", is(any(String.class)));
  70.     }
  71.  
  72.     // Cadenas
  73.  
  74.     @Test
  75.     public void startsWithExample() {
  76.         assertThat("foo bar baz", startsWith("foo"));
  77.     }
  78.  
  79.     @Test
  80.     public void endsWithExample() {
  81.         assertThat("foo bar baz", endsWith("baz"));
  82.     }
  83.  
  84.     @Test
  85.     public void constainsStringExample() {
  86.         assertThat("foo bar baz", containsString("bar"));
  87.     }
  88.  
  89.     // Booleanos
  90.  
  91.     @Test
  92.     public void allOfExample() {
  93.         assertThat("foo bar baz", allOf(startsWith("foo"), endsWith("baz")));
  94.     }
  95.  
  96.     @Test
  97.     public void anyOfExample() {
  98.         assertThat("foo bar qux", anyOf(startsWith("foo"), endsWith("baz")));
  99.     }
  100.  
  101.     @Test
  102.     public void bothExample() {
  103.         assertThat("foo bar baz", both(startsWith("foo")).and(endsWith("baz")));
  104.     }
  105.  
  106.     @Test
  107.     public void eitherExample() {
  108.         assertThat("foo bar baz", either(startsWith("foo")).or(endsWith("baz")));
  109.     }
  110.  
  111.     // Colecciones
  112.  
  113.     @Test
  114.     public void hasItemExample() {
  115.         assertThat(java.util.Arrays.asList("foo", "bar", "baz"), hasItem("bar"));
  116.         assertThat(java.util.Arrays.asList("foo", "bar", "baz"), hasItem(startsWith("b")));
  117.     }
  118.  
  119.     @Test
  120.     public void hasItemsExample() {
  121.         assertThat(java.util.Arrays.asList("foo", "bar", "baz"), hasItems("bar", "baz"));
  122.         assertThat(java.util.Arrays.asList("foo", "bar", "baz"), hasItems(endsWith("o"), endsWith("z")));
  123.     }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement