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.