Mockito在JUnit中处理void方法

huangapple go评论87阅读模式
英文:

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 替换为 employeeDAOemployeeService 被注解为 @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);

huangapple
  • 本文由 发表于 2020年10月1日 16:29:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64151647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定