2016-07-01 201 views
0

JMockit可以修改它模拟的方法的参数吗?修改它所嘲讽的方法的返回值肯定很容易,但如何修改参数本身呢?我知道有可能至少使用验证来捕获和测试模拟参数,但是这是在事实发生之后发生的。JMockit:修改模拟方法的参数

这里是我的简化代码:

class Employee{ 
    Integer id; 
    String department; 
    String status; 

    //getters and setters follow 
} 

我想的方法测试:

public int createNewEmployee() { 
     Employee employee = new Employee(); 
     employee.setDepartment("..."); 
     employee.setStatus("..."); 
     //I want to mock employeeDao, but the real DAO assigns an ID to employee on save 
     employeeDao.saveToDatabase(employee); 
     return employee.getId(); //throws NullPointerException if mocked, because id is null 
    } 

回答

2

使用分配给result领域Delegate对象,记录上employeeDao.saveToDatabase(...)的期望时。委托方法(使用任意名称)应声明参数Employee emp;那么只需拨打emp.setId(...)并输入您想要的任何id值即可。

例如,请参阅documentation

+0

这是一个强大的功能。 – user64141

相关问题