JUnit new assertion methods

Posted on 2011-04-21
Last Modified: 2012-08-13

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?
public void listEquality() {  
    List<Integer> expected = new ArrayList<Integer>();  
    List<Integer> actual = new ArrayList<Integer>();  
    assertEquals(expected, actual);  

at link
I have not clearly understood what author exactly meant. Please advise.. Any ideas, sample code, links, resources highly appreciated. thanks in advance
Question by:gudii9
    LVL 47

    Expert Comment


    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).


    LVL 7

    Author Comment

    are arrays and lists are same. why both are being treated as same way. where we are checking length. please advise
    LVL 47

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This video teaches viewers about errors in exception handling.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now