1
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?

Novaterata
  • 4,356
  • 3
  • 29
  • 51
RohanG
  • 65
  • 2
  • 2
  • 7
  • 1
    `users.getPassword().contains(password)` So if i type "e" as a password i can get into any account whose password contains an "e" nice :D – litelite Aug 05 '17 at 17:43
  • Some suggestions: Don't use Vector https://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated Also classes should be UpperCamelCase, so `user` should 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. – Novaterata Aug 05 '17 at 17:50
  • Can you please clarify? If you just want a print message when you successfully find user info correctly, say `System.out.println("User found!");`, or something like that after the line `isSuccess=true;` – Luke Thistlethwaite Aug 05 '17 at 18:00
  • @litelite We don't know what `getPassword() `returns and what `contains(password)` does so maybe it's secure... – Oleg Aug 05 '17 at 18:08

1 Answers1

3

Your code is hard to read. Learn Java coding standards.

Your method should not be printing anything. Determine if the user is valid. Print messages elsewhere.

I'll assume you've got a class User that encapsulates credentials:

package misc.user;

import org.apache.commons.lang3.StringUtils;

/**
 * Created by Michael
 * Creation date 8/5/2017.
 * @link https://stackoverflow.com/questions/45524768/java-unit-testing-help-for-loop
 */
public class User {

    private final String username;
    private final String password;

    public User(String username, String password) {
        if (StringUtils.isBlank(username)) throw new IllegalArgumentException("username cannot be blank or null");
        if (StringUtils.isBlank(password)) throw new IllegalArgumentException("password cannot be blank or null");
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) { return true; }
        if (o == null || getClass() != o.getClass()) { return false; }

        User user = (User) o;

        if (!getUsername().equals(user.getUsername())) { return false; }
        return getPassword().equals(user.getPassword());
    }

    @Override
    public int hashCode() {
        int result = getUsername().hashCode();
        result = 31 * result + getPassword().hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "{\"User\":{"
                + "\"username\":\"" + username + "\""
                + ",\"password\":\"" + password + "\""
                + "}}";
    }
}

I'd test it using JUnit:

package misc.user;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Michael
 * Creation date 8/5/2017.
 * @link https://stackoverflow.com/questions/45524768/java-unit-testing-help-for-loop
 */
public class UserTest {

    private static List<User> users;

    @BeforeClass
    public static void setUp() {
        users = new ArrayList<>();
        users.add(new User("FooBar", "myPassword"));
        users.add(new User("GeorgeBush", "exPrez"));
        users.add(new User("weatherBoy", "cloudy"));
    }

    @Test
    public void testLogin_Success() {
        // setup
        String username = "weatherBoy";
        String password = "cloudy";
        // exercise
        boolean isValidUser = users.contains(new User(username, password));
        // assert
        Assert.assertTrue(isValidUser);
    }

    @Test
    public void testLogin_Failure() {
        // setup
        String username = "noSuchUser";
        String password = "does not matter";
        // exercise
        boolean isValidUser = users.contains(new User(username, password));
        // assert
        Assert.assertFalse(isValidUser);
    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561