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

使用 Mockito 模擬 JdbcTemplate 的數據源

Mocking DataSource for JdbcTemplate with Mockito(使用 Mockito 模擬 JdbcTemplate 的數據源)
本文介紹了使用 Mockito 模擬 JdbcTemplate 的數據源的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試在 Spring 項目中測試一個類.我想在測試類與 dao 類中進行盡可能多的更改,這樣我就不必因為更改而重新測試各種東西.

I'm trying to test a class in a Spring project. I would like to make as many changes as possible in the test class vs. the dao class so that I don't have to retest all sorts things because of a change.

我正在使用的類有一個由以下實例化的 JdbcTemplate 模板 類變量:

The class I'm working with has a JdbcTemplate template class variable that is instantiated by the following:

setJdbcTemplate(DataSource dataSource) {
    this.template = new JdbcTemplate(dataSource);
}

我想測試的方法使用 template.query(<code>) 來運行已定義的 SQL 查詢并將結果返回到列表.

The method I would like to test makes a template.query(<code>) to run a defined SQL query and return the results to a list.

我在我的測試用例中創建了以下內容,但我不確定如何使用它.我可以使用 Mockito 使以下代碼返回某個字符串列表嗎?

I created the following in my test case, but I'm not sure how to put it into use. Can I make the following code return a certain list of Strings using Mockito?

DataSource mockedDataSrc = Mockito.mock(DataSource.class);
customerClassDao.setJdbcTemplate(mockedDataSrc); 

我能否以某種方式使用 when 或其他命令來設置我想要返回給 JdbcTemplate 的 .query 調用的內容?

Can I somehow use the when or another command to set what I want to be returned to the JdbcTemplate's .query call?

推薦答案

您不能這樣做,因為您無法控制 JdbcTemplate 實現.您應該依賴注入 JdbcTemplate 然后模擬 JdbcTemplate .

You can't do this because you don't have control over the JdbcTemplate implementation. You should dependency inject the JdbcTemplate and then mock the JdbcTemplate instead.

這個困難是指出你的代碼有問題.您的代碼取決于 JdbcTemplate 的具體實例.如果你在它上面使用依賴注入,它的耦合會更少.

This difficulty is pointing out a problem with your code. Your code depends on the concrete instance of JdbcTemplate. It would be less coupled if you used Dependency Injection on it instead.

由于您不想修改被測系統,您可以這樣做:

Since you don't want to modify your system under test, you can do this:

更改 template 字段,使其受到包保護(即:刪除 private 關鍵字).然后,在您實例化您正在測試的類之后,我會將其設置為模擬(JdbcTemplate.class).現在您將能夠像您最初想要的那樣直接在 JdbcTemplate 上使用和驗證.

Change the template field so it's package protected (ie: remove the private keyword). Then, I'd set it to be a mock(JdbcTemplate.class) after you instantiate the class you're testing. Now you'll be able to use when and verify on the JdbcTemplate directly like you wanted originally.

所以你正在測試的類將如下所示:

So the class you're testing will look like this:

public class SystemUnderTest {

JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(DataSource dataSource) {
        this.template = new JdbcTemplate(dataSource);
    }

}

你的測試會這樣做:

@Before
public void setUp() {
    SystemUnderTest sut = new SystemUnderTest();
    sut.jdbcTemplate = mock(JdbcTemplate.class);                
}

// ...

這篇關于使用 Mockito 模擬 JdbcTemplate 的數據源的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數據庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 成人午夜在线观看 | 天天综合操 | av资源中文在线天堂 | 青青久久 | 国产一二区视频 | 久久毛片| 久久精品色欧美aⅴ一区二区 | 91在线影院 | 亚洲风情在线观看 | 成人三级视频 | 全免一级毛片 | 国产高清免费视频 | 国产美女自拍视频 | 国产精品久久久久久婷婷天堂 | 激情在线视频网站 | 97av视频在线 | 欧美三级电影在线播放 | www成年人视频 | 欧美八区 | 国产1区2区3区 | 国产一级片在线播放 | 精品国产伦一区二区三区观看说明 | 久久精品免费观看 | 成人av一区二区三区 | 国产午夜精品久久久 | 日韩欧美在线视频 | 成人精品在线观看 | www.中文字幕 | 成人在线视频网站 | 91精品久久久久久综合五月天 | www.日韩高清 | 成人精品一区二区 | 午夜视频在线观看视频 | 青青久久| 亚洲精品一区二区久 | 久久99精品久久久久 | 欧美偷偷操 | 能看的av网站 | 久久视频精品 | 日韩av免费看 | 91精品国产欧美一区二区 |