Jefer590
Jefer590

Reputation: 23

two ArrayList with same content doesn't match in JUNIT

I have a little Problem with jUnit and ArrayList

Here's my problem:

I need to test 2 ArrayList of the same object (ArticuleED it's called) so... in jUnit I do the next code to compare the result the and actual:

PersistenciaListas instance = new PersistenciaListas();
    List<ArticuloED> actual = new ArrayList<>();
    List<ArticuloED> result;

    //Objects to add
    //Videojuego is a subclass of Articulo(superclass)
    ArticuloED articuloED1 = new ArticuloED(new Videojuego("VJ0011", "ULTRA Street Fighter 4", "Peleas", "T", "PS4", "Capcom", 5),10,10);
    ArticuloED articuloED2 = new ArticuloED(new Videojuego("10", "Super Smash Bros Project M", "Peleas", "T", "Nintendo Wii", "Team M", 3));

    //add elements to instance
    //"Inventariar" is like add to the inventory
    //That 10 is the number of items you will add to existence and availability
    instance.inventariar((Videojuego) articuloED1.getArticulo(),10);

    //Verified if they have the same elements
    //that 10 and 10 are the 10 we added at inventariar, the first number is the existence
    //the other is the availability
    resultado = instance.consultarInventarioVideojuegos();
    esperado.add(new ArticuloED(new Videojuego("VJ0011", "ULTRA Street Fighter 4", "Peleas", "T", "PS4", "Capcom", 5),10,10));
    assertEquals(actual, result);

that's the code that i use in my test class

the error that i got is the next:

expected: java.util.ArrayList<[VJ0011, ULTRA Street Fighter 4, 10, 10]> but was: java.util.ArrayList<[VJ0011, ULTRA Street Fighter 4, 10, 10]> junit.framework.AssertionFailedError at persistencia.PersistenciaListasTest.testConsultarInventarioVideojuegos(PersistenciaListasTest.java:492)

Upvotes: 2

Views: 2563

Answers (4)

Martin Schr&#246;der
Martin Schr&#246;der

Reputation: 4591

As @fedorSmirnov already wrote, you are comparing two non-primitive data types with equal, which will not do what you want.

I suggest you use AssertJ, which has asserts for Lists (and many more classes).

Upvotes: 0

fedorSmirnov
fedorSmirnov

Reputation: 711

You are comparing two non-primitive datatypes. The assertEquals method returns false because with non-primitive datatypes (in your case, the two Lists actual and result) it checks for the equality of the object references instead of the object contents.

To solve this, you can do two things:

1) You can access the primitive attributes of your objects and compare them with the assertEquals method.

2) You can write an own method performing the object comparison and then integrate it into JUnit by running

assertEquals(true, myComparison(actual, result));

with

public boolean myComparison(ArrayList<ArtuculoED> first, ArrayList<ArticuloED> second){

// return true, if contents equal and false if not

}

Upvotes: 2

Xavier Coulon
Xavier Coulon

Reputation: 1600

Your domain classes (ArticuloED and Videojuego) should override the equals() and hashCode() methods so that the content of both list are the same.

Upvotes: 2

Matthieu
Matthieu

Reputation: 3117

assertEquals will not "deep-search" both classes for equality but use the equals() method of the tested objects. Even though the toString() might return the same, you'd have to loop on each element to test for single equality:

assertEquals(actual.size(), result.size());
for (int i = 0; i < actual.size(); i++)
    assertEquals(actual.get(i), result.get(i));

Upvotes: 1

Related Questions