久久久久久久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(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 天天色图| wwwww在线观看 | 麻豆国产精品777777在线 | 亚洲一区二区三区观看 | 国产主播第一页 | 国产男女猛烈无遮掩视频免费网站 | 夜夜爽99久久国产综合精品女不卡 | 日韩国产一区二区 | 精品久久久久久 | 中文字幕日韩欧美一区二区三区 | 国产电影一区二区在线观看 | 国产a爽一区二区久久久 | 国产精品久久久久久久久久久久久久 | 欧美日韩电影在线 | 欧美中文字幕一区二区三区亚洲 | 亚洲精品一区中文字幕乱码 | 久久久国产一区 | 中文字幕色站 | 亚洲精品在线播放 | 欧美精品一区二区三区在线 | 97精品超碰一区二区三区 | 国产1区| 久久久精品网站 | www国产亚洲精品久久网站 | 国产精品毛片一区二区三区 | 亚洲在线一区二区三区 | 久久精品视频在线免费观看 | 精品一区二区免费视频 | 色综合久久88色综合天天 | 久久激情网 | 欧美在线日韩 | 欧美日韩国产在线观看 | 中文字幕一区二区三区四区五区 | 久久久爽爽爽美女图片 | 成人免费观看网站 | 久久久精品日本 | 久久av资源网 | 国产成人久久精品一区二区三区 | 日本不卡视频在线播放 | 黄色片网站在线观看 | 在线āv视频 |