簡體   English   中英

Mockito驗證方法調用的參數

[英]Mockito verify the arguments of a method invocation

在為具有多個傳入參數的方法編寫Mockito單元測試時,我想驗證是否傳入了確切的參數。方法簽名是(出於示例目的):

public void process(String stringParam, int numParam, CustomObject objectParam)

我知道這些參數必須在內部傳遞:

String stringParam = "line1 \n line2 \n line2 \n"
int numParam = 123;
AnotherCustomObject bank = new AnotherCustomObject(1, "Bank name")
CustomObject objectParam = new CustomObject(1, "Customer name", bank);

驗證方法看起來像這樣:

verify(testObject, times(1)).process(eq(stringParam), eq(numParam), eq(objectParam));

但是結果是

Argument(s) are different! Wanted:
...all the details of failure...
Comparison Failure:  <Click to see difference>
...the rest of details...

當您單擊“ 單擊以查看差異並希望看到原始問題”時,您只會看到令人困惑的消息內容是相同的 (至少在IntelliJ Idea中,您會看到此消息) 在此處輸入圖片說明

在調查此案例時,我瀏覽了幾篇文章,其中其根被視為AnotherCustomObject或CustomObject的一個錯誤覆蓋的equals()方法。

經過調查,事實證明這不是問題。 一切都變得平淡無奇:

傳遞給該方法的字符串中存在的換行符具有默認的Windows編碼( / r / n )。 同時,為了在Mockito中進行驗證,我使用了一個僅帶/ n中斷的字符串(這有點奇怪,因為我在調試方法時得到了該字符串)。

同時,Inteliji Idea在比較模式下顯示兩個字符串相等。

更新 :但最好使用
System.getProperty( “文件分割符”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM