-
Notifications
You must be signed in to change notification settings - Fork 383
Open
Description
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.
Metadata
Metadata
Assignees
Labels
No labels