Re: assertEquals fails to assert for arrays of primary data type

"Corey Vaillancourt [email protected] [junit]" <[email protected]> Sun, 17 May 2015 18:28:33 -0400
Newsgroups gmane.comp.java.junit.user
Message-ID <[email protected]>
Hi,

It would be more appropriate to use Assert.assertArrayEquals (http://junit.sourceforge.net/javadoc/org/junit/Assert.html) than assertEquals. Another alternative would be to use one of hamcrest' array marchers (http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html). 

I hope this helps!

Corey

> On May 17, 2015, at 10:00, XueFeng [email protected] [junit] <[email protected]> wrote:
> 
> Hi,
> 
> I have tried JUnit 4.12. It seems that the assertion assertEquals for array does not work well. The following test is to report a failure:
> 
>     @Test
>     public void testSame1(){
>         int[] arr1 = new int[]{1, 2, 3};
>         int[] arr2 = new int[]{1, 2, 3};
>         assertEquals(arr1, arr2);
>     }
> 
> While another test is passed:
> 
>     @Test
>     public void testSame2(){
>         String[] arr1 = new String[]{"Hello", "world"};
>         String[] arr2 = new String[]{"Hello", "world"};
>         assertEquals(arr1, arr2);
>     }
> 
> Two arrays of the first test contain integers, the second test's contain objects. I wonder the assertEquals compares two arrays of primary data type by reference, while compares two arrays of class by their content. Is it right? If so, how can I compare two arrays of primary data type by their content? thanks!
> 
>