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?

4
  • 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 Commented Aug 5, 2017 at 17:43
  • Some suggestions: Don't use Vector stackoverflow.com/questions/1386275/… 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. Commented Aug 5, 2017 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; Commented Aug 5, 2017 at 18:00
  • @litelite We don't know what getPassword() returns and what contains(password) does so maybe it's secure... Commented Aug 5, 2017 at 18:08

1 Answer 1

4

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.