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

用數據填充 ResultSet 的簡單方法

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

問題描述

我想模擬一個 ResultSet.嚴重地.我正在重構一大段復雜的代碼,它正在解析來自 ResultSet 的數據,并且我希望我的代碼具有相同的行為.所以,我需要為正在重構的部分編寫一個單元測試,以便能夠對此進行測試.

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

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

Second approach looks somewhat easier and much more supportable.

對于創建這樣的模擬,您有什么建議?(盡管有醫生,當然:-)?我錯過了眉毛一些靈丹妙藥嗎?可能,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?

推薦答案

據我所知,DBUnit 不提供結果集,盡管它可以很好地幫助您填充內存數據庫.

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

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

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.

我會改為實現一個結果集接口,或者創建一個結果集接口的動態代理到一個實現您關心的方法的類,而不必實現整個結果集.您可能會發現維護一個類就像維護一個內存數據庫一樣容易(前提是被測數據集是一致的),而且可能更容易調試.

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 拍攝結果集的快照,并讓 dbunit 在測試期間從 xml 讀取它,并讓您的虛擬結果集從 dbunit 的類中讀取數據.如果數據稍微復雜,這將是一種合理的方法.

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.

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

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

這篇關于用數據填充 ResultSet 的簡單方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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狠狠干| 欧美激情自拍 | 国产极品在线观看 | 亚洲在线免费观看 | 日韩精品视频在线 | 成人一级毛片 | 中文日韩欧美 | 国产对白videos麻豆高潮 | 超碰人人艹 | 免费黄色一级视频 | av一级在线 | 国产伦精品一区二区免费 | 成人做受黄大片 | 久久精品一区二区三区四区五区 | 国产精品黄色 | 国产精品少妇 | 成人免费看片在线观看 | 日韩av免费在线观看 | 亚洲看片 | 欧美激情一区二区 | 亚洲综合影院 | 国产视频一区二区三区四区 | 精品少妇av| 免费黄色av | 亚洲黄色在线视频 | 成人毛片在线播放 | 国产精品国产精品国产专区不片 | 伊人久久综合 | av最新网址| 日韩欧美在线一区二区 | 特级丰满少妇一级aaaa爱毛片 | 午夜美女福利视频 | 手机看片福利永久 | 青青操影院| 五月婷婷综合网 | 中文字幕免费在线观看 | 视频一区二区在线播放 | 国产美女视频网站 | 一区二区三区在线播放 |