public void login(String username, String password) {
for(int i = 0; i < Users.size(); i++) {
user users = (user) Users.get(i);
if(users.getUsername().contains(username)
&& users.getPassword().contains(password)) {
userName = users.getUsername();
userLevel = users.getUserLevel();
isSuccess = true;
}
}
}
Hello everyone. I'm trying to do a java unit testing for this method using Java Junit. But i don't know how to do that? Because there's a for loop.
Let me explain the method.
for(int i=0;i<Users.size();i++){
This "Users" is a vector. This loop runs unit this vector ends.
user users = (user) Users.get(i);
Then im calling user class for user instance.
if((users.getUsername().contains(username)) &&
(users.getPassword().contains(password))) {
Then if any of the users that matches with the values in the vectors, this gives the output.
Can anyone tell me how to write a unit test for this?
users.getPassword().contains(password)So if i type "e" as a password i can get into any account whose password contains an "e" nice :Dusershould be User. Don't have a variable called userName and a parameter called username in the same scope. Lastly, please please do not use this code for anything real, passwords or even usernames do not work like this.System.out.println("User found!");, or something like that after the lineisSuccess=true;getPassword()returns and whatcontains(password)does so maybe it's secure...