Posted In: Hamcrest, Junit, Testing

Hamcrest – containsInAnyOrder syntax

Sample using Hamcrest shows following way to use the code

 
containsInAnyOrder(Arrays.asList(equalTo("junit"))

So I expected even following should be possible. This code does not work as is.

 
List<Matcher<String>> list = new ArrayList<Matcher<String>>();
list.add(equalTo("junit"));

Hamcrest 1.3

<dependency>
	<groupId>org.hamcrest</groupId>
	<artifactId>hamcrest-library</artifactId>
	<version>1.3</version>
	<scope>test</scope>
</dependency>

Classes used from Hamcrest

import org.hamcrest.collection.IsIterableContainingInAnyOrder;

Using ErrorCollector so that execution continues even if assert statement fails.

package com.example.testing.hamcrest;

import static org.hamcrest.CoreMatchers.equalTo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hamcrest.Matcher;
import org.hamcrest.collection.IsIterableContainingInAnyOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

public class TestHamcrestCollection5 {

	@Rule
	public ErrorCollector collector = new ErrorCollector();

	@Test
	public void testMatchers() {

		try {
			Map<String, String> actual = new LinkedHashMap<>();
			actual.put("j", "junit");
			actual.put("h", "hamcrest");
			actual.put("m", "mysql");
			actual.put("o", "oracle");

			List<Matcher<String>> list = new ArrayList<Matcher<String>>();
			list.add(equalTo("junit"));
			list.add(equalTo("hamcrest"));
			list.add(equalTo("mysql"));
			list.add(equalTo("oracle"));

			Matcher[] matchers = { equalTo("junit"), equalTo("hamcrest"),
			        equalTo("mysql"), equalTo("oracle") };

			// positive - equality
			collector.checkThat(actual.values(),
			        IsIterableContainingInAnyOrder.containsInAnyOrder(
			                Arrays.asList(equalTo("junit"), equalTo("hamcrest"),
			                        equalTo("mysql"), equalTo("oracle"))));

			// Negative - Should have worked
			collector.checkThat(actual.values(), IsIterableContainingInAnyOrder
			        .containsInAnyOrder(Arrays.asList(matchers)));

			// Negative - Should have worked
			collector.checkThat(actual.values(),
			        IsIterableContainingInAnyOrder.containsInAnyOrder(list));

			// Positive
			collector.checkThat(actual.values(), IsIterableContainingInAnyOrder
			        .containsInAnyOrder(matchers));

			// Positive
			collector.checkThat(actual.values(),
			        IsIterableContainingInAnyOrder.containsInAnyOrder(
			                list.toArray(new Matcher[list.size()])));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

Output

java.lang.AssertionError: 
Expected: iterable over [<["junit", "hamcrest", "mysql", "oracle"]>] in any order
     but: Not matched: "junit"
	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
	at org.junit.Assert.assertThat(Assert.java:956)

java.lang.AssertionError: 
Expected: iterable over [<["junit", "hamcrest", "mysql", "oracle"]>] in any order
     but: Not matched: "junit"
	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
	at org.junit.Assert.assertThat(Assert.java:956)

by , on August 17th, 2017

  • Categories