Heads Up: It is my first post here, please excuse any missing information or the really novice questions.
So I am currently trying to write jUnit tests for the already finished web application that uses spring (everything works, I just have to get full coverage with the tests). I have the classes: "Employee", "EmployeeController" and "EmployeeManagement". I want to test the "registerNew" function which creates a new Employee with the filled form "EmployeeRegistrationForm" if it has no errors ("Errors result").
Now I want to write a Test for this to make sure that the function really does create a new object "Employee" which should be saved in the "EmployeeRepository" with said form.
However, I cannot seem to be able to create a filled "EmployeeForm" since it is abstract and cannot be instantiated. Therefore I am struggling to give any argument to that function and do not know how to pass the information needed for the test to function being tested.
@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();
}
答案 0 :(得分:0)
但是,我似乎无法创建一个已填充的“ EmployeeForm”。因为它是抽象的且无法实例化。
使用Mockito实例化您的抽象类。 您可以像这样使用它:
EmployeeForm form = mock(EmployeeForm.class);
现在您有一个EmployeeForm
实例,可以将其传递给您的方法。如果您需要从模拟中调用某些方法,则可以执行以下操作:
given(form.getFirstname()).willReturn("John");
通过这种方式,表单将按照您想要的方式运行。
注意:mock()
来自org.mockito.Mockito
,而given
来自org.mockito.BDDMockito
。