Java单元测试帮助循环

时间:2017-08-05 17:39:27

标签: java unit-testing junit

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

大家好。我正在尝试使用Java Junit对此方法进行java单元测试。但我不知道该怎么做?因为有一个for循环。

让我解释一下这个方法。

for(int i=0;i<Users.size();i++){

这&#34;用户&#34;是一个向量。这个循环运行单位这个向量结束。

user users = (user) Users.get(i);

然后我为用户实例调用用户类。

 if((users.getUsername().contains(username)) &&
    (users.getPassword().contains(password))) {

然后,如果任何与向量中的值匹配的用户,则给出输出。

有谁能告诉我如何为此编写单元测试?

1 个答案:

答案 0 :(得分:3)

您的代码很难阅读。了解Java coding standards

您的方法不应该打印任何东西。确定用户是否有效。在别处打印消息。

我假设您有一个封装凭据的类用户:

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 + "\""
                + "}}";
    }
}

我使用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);
    }
}