簡體   English   中英

Mockito 不返回預期的字符串,而是返回模擬對象

[英]Mockito not returning expected String instead it returning mocked object

我在嘲笑InetAddress ,當我嘲笑這個時,我得到的是模擬對象而不是我預期的字符串

    InetAddress inetAddress = Mockito.mock(InetAddress.class);
    Mockito.when(inetAddress.getHostName()).thenReturn("test")
    System.out.printinetAddress.getHostName());

我認為它應該在控制台中打印test但它正在打印模擬對象哈希碼。

Mockito.when(inetAddress.getHostName()).thenReturn("test")

使用這一行,您是在說當我調用 inetAddress.getHostName() 返回我“測試”時的模擬。 但是,您已經完成了這個聲明,您沒有調用這個模擬方法來查看會發生什么,而是打印了模擬對象,該對象引用了一個地址,就像 java 中的所有其他對象一樣。 要查看結果,在創建模擬對象后,您應該:

System.out.println(inetAddress.getHostName());

暫無
暫無

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

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