Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


JUnit new assertion methods

Posted on 2011-04-21
Medium Priority
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
  • 2
LVL 47

Expert Comment

ID: 35445159

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



Author Comment

ID: 35708188
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

for_yan earned 2000 total points
ID: 35708314
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

564 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