如何解决“参数不同”错误-单元测试

时间:2018-09-06 14:03:38

标签: java junit mockito

我想使用Mockito为我的 addTask 方法编写单元测试。 这是包含此方法的类。

package controller;

import model.Task;
import model.User;
import repository.TaskActions;
import repository.UserActions;

import java.sql.SQLException;
import java.util.List;

public class ToDoEngine {
private TaskActions taskActions;
private UserActions userActions;
private User connectedUser;

public ToDoEngine(UserActions userStorage, TaskActions taskStorage) {
    this.taskActions = taskStorage;
    this.userActions = userStorage;
}

public boolean signIn(String username, String password) throws SQLException {
    connectedUser = new User(username, password);
    if (!userActions.signIn(connectedUser)) {
        return false;
    }
    connectedUser.setID(retrieveConnectedUserID(connectedUser));
    return true;
}

private int retrieveConnectedUserID(User connectedUser) throws SQLException {
    return userActions.retrieveUserID(connectedUser);
}

public void addTask(String taskName) throws SQLException {
    taskActions.addTask(new Task(taskName), connectedUser);
}

}

这是我的尝试。不幸的是,我有错误。下面,我要向您介绍stacktrace:

package controller;

import model.Task;
import model.User;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import repository.TaskActions;
import repository.UserActions;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class ToDoEngineTest {

@Mock
TaskActions taskActionsMock;
@Mock
UserActions userActionsMock;
private ToDoEngine toDoEngine;


@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    toDoEngine = new ToDoEngine(userActionsMock, taskActionsMock);
}

@Test
public void addTask() throws SQLException {
    Task taskName = new Task("wash");
    User user = new User("admin","123");
    toDoEngine.addTask("wash");
    verify(taskActionsMock).addTask(taskName,user);
}
}

堆栈跟踪

Argument(s) are different! Wanted:
taskActionsMock.addTask(
    Task(taskName=wash),
    model.User@1b71f500
);
-> at controller.ToDoEngineTest.addTask(ToDoEngineTest.java:68)
Actual invocation has different arguments:
taskActionsMock.addTask(
    Task(taskName=wash),
    null
);
-> at controller.ToDoEngine.addTask(ToDoEngine.java:40)

Comparison Failure:  <Click to see difference>

Argument(s) are different! Wanted:
taskActionsMock.addTask(
    Task(taskName=wash),
    model.User@1b71f500
);
-> at controller.ToDoEngineTest.addTask(ToDoEngineTest.java:68)
Actual invocation has different arguments:
taskActionsMock.addTask(
    Task(taskName=wash),
    null
);
-> at controller.ToDoEngine.addTask(ToDoEngine.java:40)
at controller.ToDoEngineTest.addTask(ToDoEngineTest.java:68)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
...

用户和任务类包含hashCode和Equals方法。

任务

package model;

import lombok.*;

@Getter
@Setter
@AllArgsConstructor
@ToString
@NoArgsConstructor
@EqualsAndHashCode
public class Task {
private String taskName;
}

用户

package model;

import lombok.*;

@RequiredArgsConstructor
@Getter
@Setter
@EqualsAndHashCode

public class User {

private final String name;
private final String password;
private int ID;
}

预先感谢您的帮助。 :D

2 个答案:

答案 0 :(得分:1)

您的verify方法指定您希望通过特定的addTasktaskName对象调用user

verify(taskActionsMock).addTask(taskName,user);

但是由于您的连接用户是null,因此此期望无法实现。 如果您不关心连接的用户,则可以使用匹配器告诉Mockito忽略其实际值。例如

verify(taskActionsMock).addTask(ArgumentMatchers.eq(taskName), ArgumentMatchers.any());

或者,如果您确实关心用户,则只需将ToDoEngine设置为已连接用户即可。

答案 1 :(得分:1)

在测试用例中,您试图验证对该方法的调用:

public void addTask(String taskName) throws SQLException {
    taskActions.addTask(new Task(taskName), connectedUser);
}

与此:

User user = new User("admin","123");
...
verify(taskActionsMock).addTask(taskName,user);

失败消息...

Actual invocation has different arguments:
taskActionsMock.addTask(
    Task(taskName=wash),
    null
);

...告诉我们测试调用中connectedUser的值为null

在您的代码中,connectedUser的{​​{1}}成员是通过调用ToDoEngine方法来填充的,但是您的测试用例并未调用该方法,因此signIn()是测试connectedUser调用时为null。

因此,如果您不需要/不想测试是否向addTask提供了正确的用户,则只需将您的验证调用更改为:addTask

但是,这似乎是一个回避,因此您应确保verify(taskActionsMock).addTask(taskName,null)不为null,并且应为您在测试用例中提供给verify调用的值。