5

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));
Johnyb
  • 980
  • 1
  • 12
  • 27
  • Please share what exact error do you get - compile or runtime one. It would be also useful to add stacktrace of error that you encountered – Daniel Jun 23 '21 at 23:28
  • @Daniel added comment about it! – Johnyb Jun 23 '21 at 23:29
  • Please provide reproducable example with all input and output - as it will help to determine the root cause – Daniel Jun 23 '21 at 23:31
  • `assertThat(output).hasSameElementsAs(output1)` This is what you are looking for if I understand the problem correctly. – Peter Lustig Jun 23 '21 at 23:59

1 Answers1

4

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())));
rimesc
  • 158
  • 2
  • 7
  • 2
    while first part of explanation is right - example do not follows these rules. Next code should do valid assertion: assertThat(output, Matchers.containsInAnyOrder(output1.toArray())); – Daniel Jun 23 '21 at 23:50
  • The example I gave works for me with the data in the question (now I've fixed the missing bracket) assuming you replace `data` with `output1`. What error are you getting? – rimesc Jun 24 '21 at 00:15
  • Thank you ever so much - I was facing this precise issue and following your guidance has resolved my issue. – MarkAddison Oct 13 '22 at 10:03