2016-07-01 2 views
0

JMockit이 조롱 한 메소드의 매개 변수를 수정할 수 있습니까? 모의 메소드의 반환 값을 수정하는 것이 확실하지만 매개 변수 자체를 수정하는 방법은 무엇입니까? 적어도 Verification을 사용하여 조롱 된 매개 변수를 캡처하고 테스트 할 수는 있지만이 사실이 발생한 후에 발생합니다.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 매개 변수를 선언해야합니다. 원하는 id 값으로 emp.setId(...)으로 전화하면됩니다.

예를 들어, documentation을 참조하십시오.

+0

강력한 기능입니다. – user64141

관련 문제