Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to test the ordering of collection or list elements in junit?

Sometimes you may want to check whether given Collection has elements in the specific order or not. Using IsIterableContainingInOrder Class, you can address this use case.

 


CollectionElementsOrderTest.java

package com.sample.app;

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

import org.hamcrest.collection.IsIterableContainingInOrder;
import org.junit.Assert;
import org.junit.Test;

public class CollectionElementsOrderTest {

@Test
public void collectionTest() {
final List actual = Arrays.asList(11, 3,7, 5, 2);
Collections.sort(actual);

Assert.assertThat(actual , IsIterableContainingInOrder.contains(2, 3, 5, 7, 11));
}

}

 

 

 

Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

How to test the ordering of collection or list elements in junit?

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×