RohanG
RohanG

Reputation: 65

Java Unit Testing Help for loop

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?

Upvotes: 1

Views: 24315

Answers (1)

duffymo
duffymo

Reputation: 308733

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);
    }
}

Upvotes: 3

Related Questions