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

用數(shù)據(jù)填充 ResultSet 的簡單方法

Easy way to fill up ResultSet with data(用數(shù)據(jù)填充 ResultSet 的簡單方法)
本文介紹了用數(shù)據(jù)填充 ResultSet 的簡單方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想模擬一個 ResultSet.嚴重地.我正在重構(gòu)一大段復(fù)雜的代碼,它正在解析來自 ResultSet 的數(shù)據(jù),并且我希望我的代碼具有相同的行為.所以,我需要為正在重構(gòu)的部分編寫一個單元測試,以便能夠?qū)Υ诉M行測試.

I want to mock a ResultSet. Seriously. I'm refactoring one big complicated piece of code which is parsing data from ResultSet, and I want my code to behave identically. So, I need to write a unit test for the piece being refactored to be able to test this.

谷歌搜索后,我想出了兩個想法:

After googling I came up with 2 ideas:

  1. 使用 EasyMock,編寫 looooong 模擬序列.非常糟糕的解決方案:難以添加初始數(shù)據(jù)、難以更改數(shù)據(jù)、大量的測試調(diào)試.
  2. 使用 Apache Derby 或 HSQLDB 創(chuàng)建內(nèi)存數(shù)據(jù)庫,從文件或字符串?dāng)?shù)??組中填充它,使用一些神奇的 InMemoryDBUtils.query(sql) 進行查詢.然后使用該結(jié)果集.不幸的是,我沒有找到任何神奇的 InMemoryDBUtils 來快速編寫測試:-).IBM 文章使用 Derby 對持久性進行隔離單元測試"似乎正好滿足了我的需求,不過......

第二種方法看起來更簡單,更容易支持.

Second approach looks somewhat easier and much more supportable.

對于創(chuàng)建這樣的模擬,您有什么建議?(盡管有醫(yī)生,當(dāng)然:-)?我錯過了眉毛一些靈丹妙藥嗎?可能,DBUnit 就是這個工具?

What would you advice for creating such a mock? (despite doctors, of course :-)? Am I missing an eyebrow some silver bullet? Possibly, DBUnit is the tool for this?

推薦答案

據(jù)我所知,DBUnit 不提供結(jié)果集,盡管它可以很好地幫助您填充內(nèi)存數(shù)據(jù)庫.

DBUnit doesn't present a result set, to my knowledge, although it will well help you populate your in memory database.

我會說在這一點上模擬框架是錯誤的方法.模擬是關(guān)于測試行為和交互,而不僅僅是返回數(shù)據(jù),因此它可能會妨礙您.

I would say that a mocking framework is the wrong approach at this point. Mocking is about testing behavior and interaction, not just returning data, so it will likely get in your way.

我會改為實現(xiàn)一個結(jié)果集接口,或者創(chuàng)建一個結(jié)果集接口的動態(tài)代理到一個實現(xiàn)您關(guān)心的方法的類,而不必實現(xiàn)整個結(jié)果集.您可能會發(fā)現(xiàn)維護一個類就像維護一個內(nèi)存數(shù)據(jù)庫一樣容易(前提是被測數(shù)據(jù)集是一致的),而且可能更容易調(diào)試.

I would instead either implement a result set interface, or create a dynamic proxy of a result set interface to a class that implements the methods you care about without having to implement the whole result set. You will likely find maintaining a class as easy as maintaining an in memory database (provided that the dataset under test is consistent), and probably easier to debug.

您可以使用 DBUnit 備份該類,在其中使用 dbunit 拍攝結(jié)果集的快照,并讓 dbunit 在測試期間從 xml 讀取它,并讓您的虛擬結(jié)果集從 dbunit 的類中讀取數(shù)據(jù).如果數(shù)據(jù)稍微復(fù)雜,這將是一種合理的方法.

You could back up that class with DBUnit, where you take a snapshot of your result set with dbunit, and have dbunit read it back during the test from xml, and have your dummy result set read the data from dbunit's classes. This would be a reasonable approach if the data was mildly complex.

如果類是如此耦合以至于它們需要讀取作為同一測試的一部分而修改的數(shù)據(jù),我會選擇內(nèi)存數(shù)據(jù)庫.即便如此,我還是會考慮使用真實數(shù)據(jù)庫的副本,直到您設(shè)法分離該依賴項.

I would go for the in memory database if the classes were so coupled that they need to read data that was modified as part of the same test. Even then, I would consider using a copy of the real database until you managed to pull that dependency apart.

一個簡單的代理生成方法:

A simple proxy generation method:

private static class SimpleInvocationHandler implements InvocationHandler {
    private Object invokee;

    public SimpleInvocationHandler(Object invokee) {
        this.invokee = invokee;
    }

    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        method = invokee.getClass().getMethod(method.getName(), method.getParameterTypes());
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        try {
            return method.invoke(invokee, args);
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
    }
}

public static <T> T generateProxy(Object realObject, Class... interfaces) {
    return (T) Proxy.newProxyInstance(realObject.getClass().getClassLoader(), interfaces, new SimpleInvocationHandler(realObject));
}

這篇關(guān)于用數(shù)據(jù)填充 ResultSet 的簡單方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 - 無法投射模擬)
Set value to mocked object but get null(將值設(shè)置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調(diào)用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 国产aⅴ爽av久久久久久久 | 最新日韩在线视频 | 黄色av网站在线观看 | 国产成人精品一区 | 一区二区三区亚洲 | 亚洲网站在线观看 | 国产精品久久一区二区三区 | 精品久久精品 | 欧美日韩国产一区二区三区 | 中文在线一区二区 | 欧美啊v在线观看 | 国产精品久久久久久亚洲调教 | 久久久久国产精品午夜一区 | 国产欧美日韩视频 | 国产在线观看不卡一区二区三区 | 欧美日韩综合视频 | 欧美一二区 | 亚洲精品视频一区二区三区 | 毛片视频免费观看 | 欧美视频在线看 | 国产乡下妇女做爰 | 一区二区日韩精品 | 丁香综合| 国产日韩欧美在线观看 | 精品美女视频在线观看免费软件 | 日产精品久久久一区二区福利 | 免费高清av | 亚洲一级黄色 | 黄色一级电影在线观看 | 一本大道久久a久久精二百 国产成人免费在线 | 国产99久久 | 国产一区二区三区视频免费观看 | 91久久精品一区二区二区 | 欧美日韩视频在线第一区 | 青青久草 | 欧美freesex黑人又粗又大 | 亚洲欧美中文日韩在线v日本 | 日本a∨视频 | 日韩中文字幕一区 | 久久久久国产成人精品亚洲午夜 | 九九亚洲精品 |