Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casted Type should not matter for containsInAnyOrder #301

Open
thigg opened this issue May 2, 2020 · 4 comments
Open

Casted Type should not matter for containsInAnyOrder #301

thigg opened this issue May 2, 2020 · 4 comments

Comments

@thigg
Copy link

thigg commented May 2, 2020

Regard following Example:

import java.util.Arrays;

import org.hamcrest.Matchers;
import org.hamcrest.core.AnyOf;
import org.junit.Test;

import static org.junit.Assert.assertThat;


public class TestHamcrest {
    @Test
    public void hamcrestTest() {
        new AnyOf<>(null, null);
        String[] truth = new String[]{"1", "2"};
        assertThat(Arrays.asList("1", "2"), Matchers.containsInAnyOrder(truth));
        Object otherTruth = truth;
        assertThat(Arrays.asList("1", "2"), Matchers.containsInAnyOrder(otherTruth));
    }
}

Result:

java.lang.AssertionError: 
Expected: iterable over [["1", "2"]] in any order
     but: Not matched: "1"

	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
	at org.junit.Assert.assertThat(Assert.java:956)
	at org.junit.Assert.assertThat(Assert.java:923)
	at or.example.Example.hamcrestTest(Example.java:22)

The first assert is successfull the second isn't.
This leads to very subtle bugs.

Affected:
Version: junit:4.12 with hamcrest:1.3 (java-8-openjdk)
and
Version: junit:4.12 with hamcrest:2.2 (1.3 excluded) (java-13-openjdk)

Actual Behavior:
If an array is casted to an object, containsInAnyOrder (and most probably others) regards it as a single item. Thus it asserts that the array ["1","2"] is in the results.

Expected Behavior:
Both calls should behave the same.

@nhojpatrick
Copy link
Member

@thigg please retest with org.hamcrest:hamcrest:2.2 as we have no plans to do any 1.x releases.

@thigg
Copy link
Author

thigg commented May 5, 2020

Still valid in 2.2. Updated first comment. Confirmed correct version via:

     <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest</artifactId>
            <version>2.2</version>
            <scope>test</scope>
        </dependency>

(confirmed with dependency:tree)

and new AnyOf<>(null, null); which is a 2.2 feature according to #245

@gokareless
Copy link

@nhojpatrick could you please review #317

@gokareless
Copy link

gokareless commented Feb 12, 2021

@ anyone?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants