Skip to content

Conversation

@pimterry
Copy link
Contributor

@pimterry pimterry commented Feb 9, 2013

Assert has a range of assertArrayEquals() methods for different types, including char[]/object[]/byte[] etc, but no method to compare equality for boolean[], which this patch now adds.

The filed issue for this (#86) also complains about missing float[] and double[] equality methods, but those appear to now exist anyway, so I've ignored that.

@dsaff
Copy link
Member

dsaff commented Feb 11, 2013

Looks reasonable. Can you also add tests to complement the tests for the int-based assertArrayEquals? Thanks.

@pimterry
Copy link
Contributor Author

Done.

@dsaff
Copy link
Member

dsaff commented Feb 12, 2013

Thanks!

dsaff pushed a commit that referenced this pull request Feb 12, 2013
Added boolean assertArrayEquals to Assert, fixing #86
@dsaff dsaff merged commit 63acecd into junit-team:master Feb 12, 2013
@dsaff
Copy link
Member

dsaff commented Feb 12, 2013

@pimterry pimterry deleted the booleanArrayEq-#86 branch February 12, 2013 15:04
@pimterry
Copy link
Contributor Author

Done.

@ghost
Copy link

ghost commented Aug 1, 2014

Could we just use the same of "assertEquals(o1, o2)? instead of "assertArrayEquals", to keep consistent and concise. personally, i dislike something like: assertArrayEquals, assertCollectionEquals...for my tests, i only care about if the two objects are equals, not matter they're array, or collection, or something else.

@dsaff
Copy link
Member

dsaff commented Aug 1, 2014

@lihy70, assertEquals(o1, o2) already applies Java's definition of equals to o1 and o2. Unfortunately, this is not terribly useful in the case of arrays, so assertArrayEquals is a different method you can use to get the more useful behavior.

@ghost
Copy link

ghost commented Aug 1, 2014

first of all, let's just think about the use case, i write a test to check if my method return the result which is a int[]. for example by code: {

public void test_m1() {
int[] result = m1(); // assume result = {1, 2, 3}
int[] expected = {1, 2, 3}
assertEquals(expected, result); // By the API spec of the method assertEquals: "Asserts that two objects are equal". "true" should be returned because this is what we expected, right?
}

the assertEquals should call o1.eqauls(o2) first, if it's false, then check if o1 and o2 are array and have same class type, if yes, then call Arrays.deepEquals(o1, o2).

why do i ask this question? because sometimes i happen to use assertEquals to compare the result which is an array and get wrong result. then i looked at my test and said to myself: "fuck, can't it be better? you know what i want"

@kcooney
Copy link
Member

kcooney commented Aug 3, 2014

@lihy70 if we changed assertEquals(Object, Object) as you propose we would need to also change assertNotEquals to match. This would likely break someone.

Have you looked at Truth? See http://google.github.io/truth

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 this pull request may close these issues.

4 participants