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

使用 Mockito 模擬 JdbcTemplate 的數(shù)據(jù)源

Mocking DataSource for JdbcTemplate with Mockito(使用 Mockito 模擬 JdbcTemplate 的數(shù)據(jù)源)
本文介紹了使用 Mockito 模擬 JdbcTemplate 的數(shù)據(jù)源的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在嘗試在 Spring 項(xiàng)目中測(cè)試一個(gè)類(lèi).我想在測(cè)試類(lèi)與 dao 類(lèi)中進(jìn)行盡可能多的更改,這樣我就不必因?yàn)楦亩匦聹y(cè)試各種東西.

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.

我正在使用的類(lèi)有一個(gè)由以下實(shí)例化的 JdbcTemplate 模板 類(lèi)變量:

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);
}

我想測(cè)試的方法使用 template.query(<code>) 來(lái)運(yùn)行已定義的 SQL 查詢(xún)并將結(jié)果返回到列表.

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.

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

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 或其他命令來(lái)設(shè)置我想要返回給 JdbcTemplate 的 .query 調(diào)用的內(nèi)容?

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

推薦答案

您不能這樣做,因?yàn)槟鸁o(wú)法控制 JdbcTemplate 實(shí)現(xiàn).您應(yīng)該依賴(lài)注入 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.

這個(gè)困難是指出你的代碼有問(wèn)題.您的代碼取決于 JdbcTemplate 的具體實(shí)例.如果你在它上面使用依賴(lài)注入,它的耦合會(huì)更少.

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.

由于您不想修改被測(cè)系統(tǒng),您可以這樣做:

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

更改 template 字段,使其受到包保護(hù)(即:刪除 private 關(guān)鍵字).然后,在您實(shí)例化您正在測(cè)試的類(lèi)之后,我會(huì)將其設(shè)置為模擬(JdbcTemplate.class).現(xiàn)在您將能夠像您最初想要的那樣直接在 JdbcTemplate 上使用和驗(yàn)證.

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.

所以你正在測(cè)試的類(lèi)將如下所示:

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);
    }

}

你的測(cè)試會(huì)這樣做:

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

// ...

這篇關(guān)于使用 Mockito 模擬 JdbcTemplate 的數(shù)據(jù)源的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to mock super reference (on super class)?(如何模擬超級(jí)參考(在超級(jí)類(lèi)上)?)
Java mock database connection(Java 模擬數(shù)據(jù)庫(kù)連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無(wú)法投射模擬)
Set value to mocked object but get null(將值設(shè)置為模擬對(duì)象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調(diào)用驗(yàn)證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 色综合久 | 国产高清视频 | 久久久五月天 | 欧美日韩在线观看视频网站 | 欧美精品久久久 | 亚洲色图网址 | 免费在线观看av网址 | 亚洲 欧美 激情 另类 校园 | 很很干很很日 | 国产区在线观看 | 久久久青草 | 黄色在线观看国产 | 色婷婷精品久久二区二区蜜臂av | 亚洲精品国产a久久久久久 午夜影院网站 | www精品美女久久久tv | 一级一级一级毛片 | 中文字幕日韩欧美一区二区三区 | 亚洲电影免费 | 视频第一区 | 成人免费视频在线观看 | 国产高清视频在线 | 国产中文字幕在线 | av天天看| 日韩精品成人免费观看视频 | 成人免费在线视频 | 亚洲视频中文字幕 | 精品久久久久久久久久久久久久 | 亚洲第一成人av | 91社区在线高清 | 91在线电影| 97视频精品 | 国产99久久精品一区二区300 | 欧美 日韩 国产 成人 在线 | a黄视频 | 色伊人久久| 久久久精品一区二区三区 | 精品九九九 | 超碰91在线 | 999www视频免费观看 | 亚洲 欧美 日韩在线 | 日本a∨视频 |