Java 如何使用参数形式测试控制器函数?
提醒:这是我在这里的第一篇帖子,请原谅任何遗漏的信息或真正的新手问题 因此,我目前正在尝试为已经完成的使用spring的web应用程序编写jUnit测试(一切都正常,我只需要对测试进行全面覆盖)。 我的课程有:“雇员”、“雇员控制者”和“雇员管理”。 我想测试“registerNew”函数,如果没有错误(“错误结果”),该函数将使用填写的表单“EmployeeRegistrationForm”创建新员工 现在,我想为此编写一个测试,以确保函数确实创建了一个新对象“Employee”,该对象应保存在“EmployeeRepository”中,并使用上述表单 然而,我似乎无法创建一个填充的“EmployeeForm”,因为它是抽象的,不能实例化。因此,我很难给出该函数的任何参数,并且不知道如何将测试所需的信息传递给正在测试的函数Java 如何使用参数形式测试控制器函数?,java,forms,spring-mvc,junit,controller,Java,Forms,Spring Mvc,Junit,Controller,提醒:这是我在这里的第一篇帖子,请原谅任何遗漏的信息或真正的新手问题 因此,我目前正在尝试为已经完成的使用spring的web应用程序编写jUnit测试(一切都正常,我只需要对测试进行全面覆盖)。 我的课程有:“雇员”、“雇员控制者”和“雇员管理”。 我想测试“registerNew”函数,如果没有错误(“错误结果”),该函数将使用填写的表单“EmployeeRegistrationForm”创建新员工 现在,我想为此编写一个测试,以确保函数确实创建了一个新对象“Employee”,该对象应保存
@Service
@Transactional
public class EmployeeManagement {
private final EmployeeRepository employees;
private final UserAccountManager userAccounts;
EmployeeManagement(EmployeeRepository employees, UserAccountManager userAccounts) {
Assert.notNull(employees, "employeeRepository must not be null!");
Assert.notNull(userAccounts, "UserAccountManager must not be null!");
this.employees=employees;
this.userAccounts = userAccounts;
}
//the function that creates the employee
public Employee createEmployee(EmployeeRegistrationForm form) {
Assert.notNull(form, "Registration form must not be null!");
String type = form.getType();
Role role = this.setRole(type);
UserAccount useraccount = userAccounts.create(form.getUsername(), form.getPassword(), role);
useraccount.setFirstname(form.getFirstname());
useraccount.setLastname(form.getLastname());
return employees.save(new Employee(form.getNumber(), form.getAddress(), useraccount));
}
@Controller
public class EmployeeController {
private final EmployeeManagement employeeManagement;
EmployeeController(EmployeeManagement employeeManagement) {
Assert.notNull(employeeManagement, "userManagement must not be null!");
this.employeeManagement = employeeManagement;
}
@PostMapping("/registerEmployee")
@PreAuthorize("hasRole('ROLE_ADMIN')")
String registerNew(@Valid EmployeeRegistrationForm form, Errors result) {
if (result.hasErrors()) {
return "registerEmployee";
}
employeeManagement.createEmployee(form);
return "redirect:/";
}
public interface EmployeeRegistrationForm {
@NotEmpty(message = "{RegistrationForm.firstname.NotEmpty}")
String getFirstname();
@NotEmpty(message = "{RegistrationForm.lastname.NotEmpty}")
String getLastname();
@NotEmpty(message = "{RegistrationForm.password.NotEmpty}")
String getPassword();
@NotEmpty(message = "{RegistrationForm.address.NotEmpty}")
String getAddress();
@NotEmpty(message = "{RegistrationForm.number.NotEmpty}")
String getNumber();
@NotEmpty(message = "{RegistrationForm.type.NotEmpty}")
String getType();
@NotEmpty(message = "{RegistrationForm.username.NotEmpty}")
String getUsername();
}
但是,我似乎无法创建一个填充的“EmployeeForm”,因为它是抽象的,无法实例化
用于实例化抽象类。
您可以这样使用它:
EmployeeForm form = mock(EmployeeForm.class);
given(form.getFirstname()).willReturn("John");
现在您有了一个EmployeeForm
的实例,可以将其传递给您的方法。如果需要从模拟中调用某些方法,可以执行以下操作:
EmployeeForm form = mock(EmployeeForm.class);
given(form.getFirstname()).willReturn("John");
这样,窗体将按照您想要的方式运行
注意:
mock()
来自org.mockito.mockito
和give
来自org.mockito.bddmocito
您的应用程序中会有一些类扩展扩展EmployeeRegistrationForm