Problem with assertEquals

3

I have problems with some tests that I am going through, specifically with "assertEquals"

I'm really comparing two "user" objects that have 3 parameters "username, password, fullname"

In the tests I do this:

    public void loginUser() {        
    Platform platform = new Platform();

    // Check that no user is in the platform
    assertEquals((int)platform.getNumUsers(), 0);

    // Register new user
    User u1=platform.registerUser(username1, password1, fullName1);
    assertNotNull(u1);
    assertEquals((int)platform.getNumUsers(), 1);

    // Try to login with the new user credentials
    User u2 = platform.login(username1, password1);
    assertNotNull(u2);
    assertEquals((int)platform.getNumUsers(), 1);

    // User information is correct
    assertEquals(u1, u2);

First I check that no user exists, then I create a user and lastly with User u2 I pass the username and password of U1 and if the information of u1 and u2 is the same I continue forward, the case is that when I come to assertEquals (u1, u2) it tells me that they are not the same but I think that if they are ...

The class where I do the calculations is the Platform class

         package edu.uoc.dpoo;
         import java.util.ArrayList;
         import java.util.List;

      /**
        *
        * @author Guillermo Rodriguez Barcelo
        */
public class Platform {

private List<Competition> competitions;
private List<User> users;

public Platform(){

    competitions = new ArrayList<Competition>();
    users = new ArrayList<User>();

}
public User registerUser(String username ,String password,String fullname){

    User p = findUser(username);
    if (p == null) {
        p =  new User(username, password,fullname);
        users.add(p);
    }
    else {
        p=null;
    }

    return (User) p;
}



public User findUser (String username) {


           User user = null;

    for (User p : users) {
        if (username.equals(p.getUserName())) {
            user = p;
                            p= null;
            break;
        }
    }



    return user;
}


public User login(String username,String password){
    User user = null;

    for (User p : users){
        if(username.equals(p.getUserName())&& (password.equals(p.getPassword())) ){          
            user = p;
            break;
    }else
    {
            return null;
            }        
    }           
    return user;
}


public Integer getNumUsers(){
    Integer  size=users.size();

    return size  ;
}
public Integer getNumCompetitions(){
    return null;
}
public Message sendMessage(User from,String to,String subject,String message){
    return null;
}
private float evaluateAll(){
    return (float) 0.0;

}

public List<Competition> getOpenCompetitions(){
    return null;
}

private User User(User u) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}





}

The code for assertEquals is as follows

    static public void assertEquals(Object expected, Object actual) {
    assertEquals(null, expected, actual);
}

The code of the assertEquals method (null, expected, current) is as follows

    static public void assertEquals(String message, Object expected,
        Object actual) {
    if (equalsRegardingNull(expected, actual)) {
        return;
    } else if (expected instanceof String && actual instanceof String) {
        String cleanMessage = message == null ? "" : message;
        throw new ComparisonFailure(cleanMessage, (String) expected,
                (String) actual);
    } else {
        failNotEquals(message, expected, actual);
    }
}



No entiendo por que me dice que los objetos u1 y u2 no son iguales.
    
asked by Roman345 13.11.2016 в 13:21
source

2 answers

2

If I have misunderstood your problem, I think you are using bad assertEquals. assertEquals, when you use it with objects, compares the objects but not the values of them. That is, two objects created separately, are two different objects.

If you want to check the values you would have to overwrite the "equals" method of your class and implement your own comparison logic.

    
answered by 14.11.2016 в 08:53
0

Well then, well, after many laps what I did was this

    @Override
public boolean equals (Object o) {
    boolean prueba = false;

    if ((o != null) && (o instanceof User)) {
        User p = (User) o;
        if (this.username == null && this.password == null && this.fullName == null) {
            prueba = (p.username == null);
                            prueba = (p.password == null);
                            prueba = (p.fullName == null);
        }
        else {
            prueba =  this.username.equals(p.username);
                            prueba =  this.password.equals(p.password);
                            prueba =  this.fullName.equals(p.fullName);
        }
    }

    return prueba;
}

Comparing parameter to parameter of each object and that worked for me ...

    
answered by 13.11.2016 в 14:53