Johnyb
Johnyb

Reputation: 1316

Hamcrest Matchers.containsInAnyOrder does not work

I am using this dependencies:

   <dependencies>
        <!-- Testing -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-all</artifactId>
                <version>1.3</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <exclusions>
                    <exclusion>
                        <artifactId>hamcrest-core</artifactId>
                        <groupId>org.hamcrest</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>1.9.5</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.hamcrest</groupId>
                        <artifactId>hamcrest-core</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>

however, using the function like:

List<String> output = ..;
List<String> data = ...;
Assert.assertThat(output, Matchers.containsInAnyOrder(data));

Results in error. WHat is the right way to fix hamcrest so the function actually works? I tried to find out correct dependency management and found the configuration mentioned above.

The errro i recieve is assertion error, if i compare two same lists but with shuffled values, it does not pass but fails

Example:

List<String> output = Arrays.asList(
                "text1\t1",
                "text2\t1",
                "text3\t1",
                "text4\t1",
                "text5\t1",
                "text6\t1",
                "text7\t1",
                "text8\t1",
                "text9\t1",
                "text10\t1"
        );

        List<String> output1 = Arrays.asList(
                "text2\t1",
                "text3\t1",
                "text4\t1",
                "text5\t1",
                "text6\t1",
                "text7\t1",
                "text8\t1",
                "text9\t1",
                "text10\t1",
                "text1\t1"
        );


        Assert.assertThat(output, Matchers.containsInAnyOrder(output1));

Upvotes: 7

Views: 3581

Answers (1)

rimesc
rimesc

Reputation: 193

The problem is Matchers.containsInAnyOrder doesn’t have a variant that accepts a collection of items to match to - it accepts either a collection of matchers or a vararg of items. In your case, you end up calling the vararg variant, so what you’re actually asserting is that output is a list of lists with a single element equal to data.

There are a few ways to make the assertion work as intended: for example, you could convert data to an array, which would satisfy the vararg, or you could map data to a collection of equalTo matchers, e.g.

Assert.assertThat(output, Matchers.containsInAnyOrder(data.stream().map(Matchers::equalTo).collect(Collectors.toList())));

Upvotes: 6

Related Questions