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

employeeDao.saveToDatabase(...)に期待を記録する際に、resultフィールドに割り当てられDelegateオブジェクトを使用します。ここ

は私の簡略化されたコードです。デリゲートメソッド(任意の名前)は、Employee empパラメータを宣言する必要があります。任意のid値を指定してemp.setId(...)と呼ぶだけです。

たとえば、documentationを参照してください。

+0

これは強力な機能です。 – user64141

関連する問題