我有以下工厂类:
public class EmployeeFactory {
public static Employee createEmployee(Employee employee) {
switch (employee) {
case DOCTOR:
return new Doctor();
case NURSE:
return new Nurse();
}
throw new EmployeeException("Invalid Employee!");
}
}
- 枚举类:**
public enum Employee {
DOCTOR("Doctor"),
NURSE("Nurse");
private final String description;
Employee(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
我想创建一个模拟枚举,例如TEACHER
,它是我的factory
方法的无效输入,以Assertexception
被抛出。
我怎么能这样做呢?
- 当前测试:**
@Test
void shouldThrowException(){
assertThrows(EmployeeException.class,
() -> {
// arrange
//todo - how to?
Employee invalidEmployee = mock(Employee.class);
when(invalidEmployee.getDescription()).thenReturn("Teacher");
// act
EmployeeFactory.createEmployee(invalidEmployee);
});
}
1条答案
按热度按时间kgsdhlau1#
你可以使用
mockStatic
,像这样嘲笑新员工: