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

containsExactly() throws ArrayIndexOutOfBoundsException (2.0M8) #138

Open
wjtk opened this issue Jan 16, 2013 · 3 comments
Open

containsExactly() throws ArrayIndexOutOfBoundsException (2.0M8) #138

wjtk opened this issue Jan 16, 2013 · 3 comments
Assignees
Labels

Comments

@wjtk
Copy link

wjtk commented Jan 16, 2013

For code:

List<String> z = new ArrayList<>(Arrays.asList("a","a","a"));       
assertThat(z).containsExactly("a","a");

Stacktrace:

java.lang.ArrayIndexOutOfBoundsException: 2
    at org.fest.assertions.internal.Iterables.assertContainsExactly(Iterables.java:995)
    at org.fest.assertions.api.AbstractIterableAssert.containsExactly(AbstractIterableAssert.java:99)

Regards, Wojtek

@joel-costigliola
Copy link
Contributor

Definitely a bug ! Thanks for reporting this.

Having said that, this assertion javadoc is misleading ... What we wanted to do with containsExactly is to assert that the actual Iterable contained all the given objects in the same order, it entails that the actual size must match the varargs size.

It means your assertion will fail (but not with an exception !), as described below :

// should fail because z has 3 elements 
assertThat(z).containsExactly("a","a");

// succeeds as given varargs is composed of 3 elements matching exactly z elements 
assertThat(z).containsExactly("a","a", "a");

You probably know that but you can use containsOnly to test z content only :

assertThat(z).containsOnly("a");

@wjtk
Copy link
Author

wjtk commented Jan 16, 2013

I know and understand :-)
Element values were not important, it could be a,b,c. I choose only "a" accidentally.

@joel-costigliola
Copy link
Contributor

fixed in 2.0M9 branch.
I'm not closing this issue because it must be merged in master branch where a major rework for 2.0 is being done, merge will happen after the rework.

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

No branches or pull requests

2 participants