久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將值設(shè)置為模擬對象但獲取 null

Set value to mocked object but get null(將值設(shè)置為模擬對象但獲取 null)
本文介紹了將值設(shè)置為模擬對象但獲取 null的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個(gè)要模擬的簡單類 Foo:

I have a simple class Foo to be mocked:

public class Foo {
   private String name;
   public Foo() {
   }
   public Foo(String name) {
     this.name = name;
   }

   public void setName(String name) {
     this.name = name;
   }
   public String getName() {
     return name;
   }
}

在我的單元測試代碼中,我使用 Mockito 來模擬它.

In my unit test code, I mock it by using Mockito.

Foo mockedFoo = Mockito.mock(Foo.class);
mockedFoo.setName("test");
// name is null
String name = mockedFoo.getName();

我在模擬對象中設(shè)置了 name,但是當(dāng)我調(diào)用 getter 獲取名稱時(shí),它返回 null.

I set name in mocked object, but when I call getter to get the name it returns null.

這是 Mockito 特有的問題,還是模擬對象無法設(shè)置值的約定?這是為什么?模擬對象下面發(fā)生了什么?

Is it a Mockito specific issue or is it an convention that mocked object can't set value? Why is that? What is happening underneath with mocked object?

推薦答案

嗯,是的 - Foo實(shí)際 代碼無關(guān)緊要,因?yàn)槟阍诔靶λ?..而 Mockito 不知道 setNamegetName 之間存在關(guān)系.它不假定它應(yīng)該將參數(shù)存儲(chǔ)到 setName 并在調(diào)用 getName 時(shí)返回它......它 可以 這樣做,但是據(jù)我所知,它沒有.Mockito 提供的 mock 只允許您指定在其上調(diào)用方法時(shí)會(huì)發(fā)生什么,并檢查以后調(diào)用了什么 .您可以模擬對 getName() 的調(diào)用,而不是調(diào)用 setName,并指定它應(yīng)該返回的內(nèi)容...

Well yes - the actual code of Foo doesn't matter, because you're mocking it... and Mockito doesn't know there's meant to be a relationship between setName and getName. It doesn't assume that it should store the argument to setName and return it when getName is called... it could do that, but it doesn't as far as I'm aware. The mock provided by Mockito just allows you to specify what happens when methods are called on it, and check what was called later on. Instead of calling setName, you could mock a call to getName() and specify what it should return...

... 或者您可以直接使用 Foo 而不是模擬它.不要認(rèn)為您必須在測試中模擬所有內(nèi)容.當(dāng)您使用真實(shí)課程時(shí),只需模擬(或偽造)尷尬的事情,例如因?yàn)樗褂梦募到y(tǒng)或網(wǎng)絡(luò).

... or you could just use Foo directly instead of mocking it. Don't think you have to mock everything in your tests. Just mock (or fake) things that are awkward when you're using the real class, e.g. because it uses the file system or network.

這篇關(guān)于將值設(shè)置為模擬對象但獲取 null的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數(shù)據(jù)庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調(diào)用驗(yàn)證方法,忽略列表中元素的順序)
Is it possible to create a mock object that implements multiple interfaces with EasyMock?(是否可以使用 EasyMock 創(chuàng)建一個(gè)實(shí)現(xiàn)多個(gè)接口的模擬對象?)
主站蜘蛛池模板: 成人在线视频免费看 | 在线黄| 日本小电影在线 | 欧美一级淫片免费视频黄 | 欧美视频成人 | 一区在线播放 | 日韩精品久久久 | 午夜视频在线视频 | 欧美一区二区在线观看视频 | 久久人人爽人人爽 | 中文字幕av一区 | 久热m3u8| 午夜丰满少妇一级毛片 | 欧美一级毛片免费观看 | 国产99久久精品一区二区永久免费 | 久久涩涩 | 天天想天天干 | 在线 丝袜 欧美 日韩 制服 | 日日夜夜天天干 | 日本三级电影免费 | 日韩欧美国产综合 | 亚洲精品一区二区在线观看 | 在线免费av电影 | 欧美一区中文字幕 | 亚洲成人av | 一级免费在线视频 | 91亚洲国产成人久久精品网站 | 美日韩精品 | 国产欧美视频一区 | 亚洲一区二区在线播放 | 亚洲第一视频 | 国产在线一区观看 | 国产午夜精品久久久 | 欧美一级欧美三级在线观看 | 精品1区| 日韩精品1区2区3区 成人黄页在线观看 | 九九国产| 日韩欧美高清 | 91免费在线视频 | 精品国产一区二区三区四区在线 | 亚洲成人精品国产 |