Monday, 26 March 2018

Hamcrest: hasSize: Checks the size of collection

By using ‘hasSize’ method, you can create a matcher that checks the size of collection.

Ex
List<Integer> employeeAges = Arrays.asList(24, 45, 32);
assertThat("Size of the collection must be equal to 3", employeeAges, hasSize(3));

Find the below working application.

TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;

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

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {

  List<Integer> employeeAges = Arrays.asList(24, 45, 32);
  assertThat("Size of the collection must be equal to 3", employeeAges, hasSize(3));

 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment