JUnit new assertion methods

Hi,

I read as

>>>

Compare arrays with new assertion methods. Two arrays are equal if they have the same length and each element is equal to the corresponding element in the other array; otherwise, they’re not.
public static void assertEquals(Object[] expected, Object[] actual);
public static void assertEquals(String message, Object[] expected, Object[] actual);

view plainprint?
@Test  
public void listEquality() {  
    List<Integer> expected = new ArrayList<Integer>();  
    expected.add(5);  
 
    List<Integer> actual = new ArrayList<Integer>();  
    actual.add(5);  
 
    assertEquals(expected, actual);  
}  

at link

http://www.cavdar.net/2008/07/21/junit-4-in-60-seconds/
I have not clearly understood what author exactly meant. Please advise.. Any ideas, sample code, links, resources highly appreciated. thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
 
for_yanCommented:
No, arrays and lists, as you probably know, are not exactly the same, for arrays we use this [] notation
for indexes and we need to create them upfront with known number of elemenet, and lists
are type of Java collections of objects where java takes care of a lot of issue for us, say
allows us to add as mmany as we want without declaring upfront how many; I'm sure you know that.
Howevere, logically arrays and lists as collecions of objects which
have certain length (number of object in the collection) and contain objects inside, have many things in common
therefore assertEquals method looks similar and when they explain how it works -
should have the same number of elements and all elements should be
pairwise equal - so the same wording allows to explain these methods.
neverthelses those are different methods - one takes arrays, another takes
lists - you should understand that these methods are different; you should
not assert equals array to list - those are separate methods for separate java animals,
which are simply related in our minds, as they have many ideas in common.

We are not checking length ourselves, but method
assertEquals starts with checking lnegths - and if lenths
of two arrays we are comparing are different than it stops and throws
assertion error, as such arrays by definition are different
If lengths are the same then this method will proceed with cvheking
element by element.
So checking length is the first stage of how these methods start executing
 
0
 
for_yanCommented:


Well, it is rather straigh forward

Let's say you declare two arrays

Object [] a = new Object[3];
Object [] b = new Object[3];

Then you populate these arrys with some Object instances.


then method

assertEquals(a, b)

will consider these arrays equal
only in case if  

(a[1].equals(b[1])  && a[2].equals(b[2]) && a[3].equals(b[3]))

all theree elements of firts array are correspondingy equal to eelemnts
of the second array

Evene before this method checks equality - it cghecks
the number of elements - if the numebr of eleemts is different
it will throw exceotion immedaiately,
if numbers of elements are the same it'll go through
all  elements and if at least one comparoison yields false
it will throw exception

Very similar thing applies to method
which compares List(s).


   




0
 
gudii9Author Commented:
are arrays and lists are same. why both are being treated as same way. where we are checking length. please advise
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.