assertThrows() 运行良好,但应该会失败
assertThrows() works well but it should fail
我是 junit 的新手,我想知道为什么 assertThrows() 在这两种情况下都能正常工作。为什么在 findById(1L) 的情况下会抛出异常?有一个 id 为 1L 的员工,因此断言应该失败。我说的对吗?
员工服务:
public Employee findById(Long id) {
return employeeRepository.findById(id)
.orElseThrow( ()-> new NoSuchElementException("not found"));
}
测试方法:
@ExtendWith(MockitoExtension.class)
class EmployeeManagerTest {
@Mock
private EmployeeRepository employeeRepository;
@InjectMocks
private EmployeeManager employeeManager;
@Test
void shouldThrowExceptionWhenEmployeeNotExist() {
Employee employee = new Employee();
employee.setId(1L);
assertThrows(NoSuchElementException.class, ()-> employeeManager.findById(123L) ); // works well
assertThrows(NoSuchElementException.class, ()-> employeeManager.findById(1L) ); // works well, but should fail, because there is an employee with id 1L
}
}
你应该嘲笑你的 EmployeeRepository
并在你的 EmployeeManager
调用存储库上的 findById
时让它 return 你的员工,或者当你 return 什么都不做想测试失败的案例。我创建了两个单独的测试:
@ExtendWith(MockitoExtension.class)
class EmployeeManagerTest {
@Mock
private EmployeeRepository employeeRepository;
@InjectMocks
private EmployeeManager employeeManager;
@Test
void shouldThrowExceptionWhenEmployeeDoesNotExist() {
// Given
// Make the repo return an empty optional when the id doesn't exit
when(employeeRepository.findById(any())).thenReturn(Optional.empty());
// Then
assertThrows(NoSuchElementException.class, ()-> employeeManager.findById(123L) );
}
@Test
void shouldReturnEmployeeWhenItExists() {
// Given
Employee employee = new Employee();
employee.setId(1L);
// Make the repo return an the employee
when(employeeRepository.findById(1L)).thenReturn(Optional.of(employee));
// When
Employee returnedEmployee = employeeManager.findById(1L);
// Then
assertEquals(employee, returnedEmployee);
}
}
我是 junit 的新手,我想知道为什么 assertThrows() 在这两种情况下都能正常工作。为什么在 findById(1L) 的情况下会抛出异常?有一个 id 为 1L 的员工,因此断言应该失败。我说的对吗?
员工服务:
public Employee findById(Long id) {
return employeeRepository.findById(id)
.orElseThrow( ()-> new NoSuchElementException("not found"));
}
测试方法:
@ExtendWith(MockitoExtension.class)
class EmployeeManagerTest {
@Mock
private EmployeeRepository employeeRepository;
@InjectMocks
private EmployeeManager employeeManager;
@Test
void shouldThrowExceptionWhenEmployeeNotExist() {
Employee employee = new Employee();
employee.setId(1L);
assertThrows(NoSuchElementException.class, ()-> employeeManager.findById(123L) ); // works well
assertThrows(NoSuchElementException.class, ()-> employeeManager.findById(1L) ); // works well, but should fail, because there is an employee with id 1L
}
}
你应该嘲笑你的 EmployeeRepository
并在你的 EmployeeManager
调用存储库上的 findById
时让它 return 你的员工,或者当你 return 什么都不做想测试失败的案例。我创建了两个单独的测试:
@ExtendWith(MockitoExtension.class)
class EmployeeManagerTest {
@Mock
private EmployeeRepository employeeRepository;
@InjectMocks
private EmployeeManager employeeManager;
@Test
void shouldThrowExceptionWhenEmployeeDoesNotExist() {
// Given
// Make the repo return an empty optional when the id doesn't exit
when(employeeRepository.findById(any())).thenReturn(Optional.empty());
// Then
assertThrows(NoSuchElementException.class, ()-> employeeManager.findById(123L) );
}
@Test
void shouldReturnEmployeeWhenItExists() {
// Given
Employee employee = new Employee();
employee.setId(1L);
// Make the repo return an the employee
when(employeeRepository.findById(1L)).thenReturn(Optional.of(employee));
// When
Employee returnedEmployee = employeeManager.findById(1L);
// Then
assertEquals(employee, returnedEmployee);
}
}