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

问题描述 投票:2回答:2

我想用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)
...

User和Task类包含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

java junit mockito
2个回答
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

查看你的代码connectedUserToDoEngine成员是通过调用signIn()方法来填充的,但是你的测试用例没有调用那个方法,因此当你的测试调用connectedUseraddTask为null。

因此,如果您不需要/想要测试是否向addTask提供了正确的用户,那么只需将您的验证调用更改为:verify(taskActionsMock).addTask(taskName,null)

但是,这感觉就像一个回避,所以你应该确保connectedUser不为null,并且是你在测试用例中提供给验证调用的值。


1
投票

你的verify方法指定你期望用特定的addTasktaskName对象调用user

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

但由于你的连接用户是null,这种期望失败了。如果您不关心已连接的用户,您可以使用匹配器告诉Mockito忽略其实际值。例如。

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

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