Monday 26 March 2018

Hamcrest: empty: Check emptiness of collection

By using ‘empty’ method, you can create a matcher that is used to check whether collection is empty or not.

Ex
List<Integer> tempList = Arrays.asList();
assertThat("tempList must be empty", tempList, empty());

Find the below working application.

TestApp.java
package com.sample.tests;

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

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

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  List<Integer> tempList = Arrays.asList();
  assertThat("empty must be empty", tempList, empty());

 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment