Reputation: 5331
Can JMockit modify the parameters of methods that it mocks? It's certainly easy to modify the return value of the method it mocks, but how about modifying the parameters themselves? I know it's possible to at least capture and test the mocked parameters using Verifications, but this happens after the fact.
Here is my simplified code:
class Employee{
Integer id;
String department;
String status;
//getters and setters follow
}
The method I want to test:
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
}
Upvotes: 1
Views: 684
Reputation: 16390
Use a Delegate
object assigned to the result
field, when recording an expectation on employeeDao.saveToDatabase(...)
. The delegate method (with an arbitrary name) should declare a Employee emp
parameter; then simply call emp.setId(...)
with whatever id value you want.
For examples, see the documentation.
Upvotes: 2