Wednesday, 4 April 2018

Hamcrest: iterableWithSize: Check the size of iterable

By using 'iterableWithSize’ method, you can create a matcher that is used to check the size of iterable.

Ex
assertThat(list, IsIterableWithSize.<Integer> iterableWithSize(3));
                           (or)
assertThat(list, IsIterableWithSize.<Integer> iterableWithSize(equalTo(3)));

Find the below working application.

TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Arrays;
import java.util.List;

import org.hamcrest.collection.IsIterableWithSize;
import static org.hamcrest.Matchers.equalTo;

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  List<Integer> list = Arrays.asList(5, 3, 2);

  assertThat(list, IsIterableWithSize.<Integer> iterableWithSize(3));
  
  // You can also compare like below.
  assertThat(list, IsIterableWithSize.<Integer> iterableWithSize(equalTo(3)));
 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment