英文:
Mockito with void method in JUnit
问题
我已经实现了一个简单的Spring Boot CRUD应用程序来编写测试用例。我在删除操作中遇到了一些问题。当我使用`Mockito.when`方法时,它期望一个返回值,但我的删除方法是无返回类型的。
**Service类**
    @Service
    public class EmployeeServiceImpl implements EmployeeService {
        private EmployeeDAO employeeDAO;
        @Autowired
        public EmployeeServiceImpl(EmployeeDAO employeeDAO) {
            this.employeeDAO = employeeDAO;
        }
        @Override
        public void deleteEmployee(Employee emp) throws IllegalArgumentException {
            employeeDAO.delete(emp);
        }
    }
**ServiceTest类**
    @ExtendWith(SpringExtension.class)
    @SpringBootTest
    public class EmployeeServiceImplTest {
        @MockBean
        private EmployeeDAO employeeDAO;
        @Autowired
        private EmployeeService employeeService;
        @Test
        public void testDeleteEmployee() {
            int empId = 1054;
            Employee employee = employee_2();
            employee.setEmpId(empId);
            // 如何为void方法编写测试用例
        }
        private Employee employee_2() {
            Employee employee = new Employee();
            employee.setEmpName("NafazBenzema");
            employee.setSalary(12000.00);
            return employee;
        }
    }
英文:
I have implemented a simple spring boot crud application to write test cases. I have faced a bit of trouble with the delete operation. When I use Mockito.when method then it is expected a return value and my delete method is non-return type.
Service Class
@Service
public class EmployeeServiceImpl  implements EmployeeService {
    private EmployeeDAO employeeDAO;
    @Autowired
    public EmployeeServiceImpl(EmployeeDAO employeeDAO)
    {
        this.employeeDAO=employeeDAO;
    }
    @Override
    public void deleteEmployee(Employee emp) throws IllegalArgumentException{
             employeeDAO.delete(emp);
    }
}
ServiceTest class
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class EmployeeServiceImplTest {
    @MockBean
    private EmployeeDAO employeeDAO;
    @Autowired
    private EmployeeService employeeService;
@Test
public void testDeleteEmployee()
{
    int empId=1054;
    Employee employee=employee_2();
    employee.setEmpId(empId);
   // how to write test case for void method
}
private Employee employee_2()
    {
        Employee employee=new Employee();
        employee.setEmpName("NafazBenzema");
        employee.setSalary(12000.00);
        return employee;
    }
}
答案1
得分: 16
你可以在你的模拟对象上使用 doNothing 或 doThrow 来模拟模拟对象的行为。
Mockito.doNothing().when(employeeDAO).delete(any());
或者
Mockito.doThrow(new RuntimeException()).when(employeeDAO).delete(any());
然而,实际上并不需要使用 doNothing,因为对于具有 void 返回类型的模拟函数,这将是默认行为。
不过你可能希望使用 verify 来验证是否调用了这个方法。例如:
verify(employeeDAO).delete(any());
英文:
You can either use doNothing or doThrow on your mock to mock the behaviour of your mock.
Mockito.doNothing().when(employeeDAO).delete(any());
or
Mockito.doThrow(new RuntimeException()).when(employeeDAO).delete(any());
However, doNothing is not actually needed as this would be the default behaviour for a mock function with a void return type.
You may want however to verify that this method was called. For example:
verify(employeeDAO).delete(any());
答案2
得分: 6
你可以使用Mockito.doNothing():
Mockito.doNothing().when(employeeDAO).deleteEmployee(any());
英文:
You can use Mockito.doNoting():
Mockito.doNothing().when(employeeDAO).deleteEmployee(any());
答案3
得分: 0
谢谢Omer。
但在我的情况下,我不得不将 employeeService 替换为 employeeDAO。employeeService 被注解为 @Autowired,并且它会抛出 NotaMockAnnotation 异常。
Mockito.doNothing().when(employeeDAO).delete(employee);
英文:
Thank you omer.
But in my case, I had to replace employeeService with employeeDAO.The employeeService is annotated with @Autowired annotation and it throws NotaMockAnnotation exception.
   Mockito.doNothing().when(employeeDAO).delete(employee);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论