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

模擬;使用列表調(diào)用驗(yàn)證方法,忽略列表中元素的

Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調(diào)用驗(yàn)證方法,忽略列表中元素的順序)
本文介紹了模擬;使用列表調(diào)用驗(yàn)證方法,忽略列表中元素的順序的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個(gè)類 (ClassA) 可以獲取目錄中的文件.它掃描給定目錄以查找匹配正則表達(dá)式的文件.對(duì)于每個(gè)匹配的文件,它會(huì)將一個(gè)文件對(duì)象添加到列表中.處理完目錄后,會(huì)將文件列表傳遞給另一個(gè)類(ClassB)進(jìn)行處理

I have a class (ClassA) that get the files in a directory. It scans the given directory for files matching a regex. For each matching file, it adds a File Object to a list. Once the directory is processed, it passes the List of Files to another Class (ClassB) for processing

我正在為 ClassA 編寫單元測(cè)試,所以我正在使用 Mockito 模擬 ClassB,并將其注入 ClassA.然后我想在不同的場(chǎng)景中驗(yàn)證傳遞給 ClassB 的列表的內(nèi)容(即我的模擬)

I am writing unit tests for ClassA, so am mocking ClassB using Mockito, and injecting it into ClassA. I then want to verify in different scenarios the contents of the list that is passed to ClassB (ie my mock)

我已將代碼剝離為以下內(nèi)容

I've stripped back the code to the following

public class ClassA implements Runnable {

    private final ClassB classB;

    public ClassA(final ClassB classB) {
        this.classB = classB;
    }

    public List<File> getFilesFromDirectories() {
        final List<File> newFileList = new ArrayList<File>();
        //        ...
        return newFileList;
    }

    public void run() {
        final List<File> fileList = getFilesFromDirectories();

        if (fileList.isEmpty()) {
            //Log Message
        } else {
            classB.sendEvent(fileList);
        }
    }
}

測(cè)試類是這樣的

    @RunWith(MockitoJUnitRunner.class)
    public class AppTest {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Mock
    private ClassB mockClassB;

    private File testFileOne;

    private File testFileTwo;

    private File testFileThree;

    @Before
    public void setup() throws IOException {
        testFileOne = folder.newFile("testFileA.txt");
        testFileTwo = folder.newFile("testFileB.txt");
        testFileThree = folder.newFile("testFileC.txt");
    }

    @Test
    public void run_secondFileCollectorRun_shouldNotProcessSameFilesAgainBecauseofDotLastFile() throws Exception {
        final ClassA objUndertest = new ClassA(mockClassB);

        final List<File> expectedFileList = createSortedExpectedFileList(testFileOne, testFileTwo, testFileThree);
        objUndertest.run();

        verify(mockClassB).sendEvent(expectedFileList);
    }

    private List<File> createSortedExpectedFileList(final File... files) {
        final List<File> expectedFileList = new ArrayList<File>();
        for (final File file : files) {
            expectedFileList.add(file);
        }
        Collections.sort(expectedFileList);
        return expectedFileList;
    }
}

問題是這個(gè)測(cè)試在 Windows 上運(yùn)行良好,但在 Linux 上失敗.原因是在windows上,ClassA列出文件的順序與expectedList相匹配,所以行

The problem is that this test works perfectly fine on windows, but fails on Linux. The reason being that on windows, the order that ClassA list the files matches the expectedList, so the line

verify(mockClassB).sendEvent(expectedFileList);

在 Windows 上會(huì)導(dǎo)致問題 expecetdFileList = {FileA, FileB, FileC},而在 Linux 上會(huì)是 {FileC, FileB, FileA},因此驗(yàn)證失敗.

is causing the problem expecetdFileList = {FileA, FileB, FileC} on Windows, whereas on Linux it will be {FileC, FileB, FileA}, so the verify fails.

問題是,我如何在 Mockito 中解決這個(gè)問題.有沒有辦法說,我希望這個(gè)方法被這個(gè)參數(shù)調(diào)用,但是我不關(guān)心列表內(nèi)容的順序.

The question is, how do I get around this in Mockito. Is there any way of saying, I expect this method to be be called with this parameter, but I don't care about the order of the contents of the list.

我確實(shí)有一個(gè)解決方案,我只是不喜歡它,我寧愿有一個(gè)更干凈、更易于閱讀的解決方案.

I do have a solution, I just don't like it, I would rather have a cleaner, easier to read solution.

我可以使用 ArgumentCaptor 獲取傳遞給模擬的實(shí)際值,然后對(duì)其進(jìn)行排序,并將其與我的預(yù)期值進(jìn)行比較.

I can use an ArgumentCaptor to get the actual value passed into the mock, then can sort it, and compare it to my expected values.

    final ArgumentCaptor<List> argument = ArgumentCaptor.forClass(List.class);
    verify(mockClassB).method(argument.capture());
    Collections.sort(expected);
    final List<String> value = argument.getValue();
    Collections.sort(value);
    assertEquals(expecetdFileList, value);

推薦答案

在另一個(gè)答案中指出,如果您不關(guān)心訂單,您可能最好更改界面,使其不關(guān)心訂單.

As noted in another answer, if you don't care about the order, you might do best to change the interface so it doesn't care about the order.

如果順序在代碼中很重要,但在特定測(cè)試中不重要,您可以像以前一樣使用 ArgumentCaptor.代碼有點(diǎn)混亂.

If order matters in the code but not in a specific test, you can use the ArgumentCaptor as you did. It clutters the code a bit.

如果這是您可能在多個(gè)測(cè)試中執(zhí)行的操作,則最好使用適當(dāng)?shù)?Mockito Matchers 或 Hamcrest Matchers,或者自己動(dòng)手制作(如果你找不到滿足需要的).hamcrest matcher 可能是最好的,因?yàn)樗梢栽?mockito 之外的其他上下文中使用.

If this is something you might do in multiple tests, you might do better to use appropriate Mockito Matchers or Hamcrest Matchers, or roll your own (if you don't find one that fills the need). A hamcrest matcher might be best as it can be used in other contexts besides mockito.

對(duì)于本示例,您可以按如下方式創(chuàng)建 hamcrest 匹配器:

For this example you could create a hamcrest matcher as follows:

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MyMatchers {
    public  static <T> Matcher<List<T>> sameAsSet(final List<T> expectedList) {
        return new BaseMatcher<List<T>>(){
            @Override
            public boolean matches(Object o) {
                List<T> actualList = Collections.EMPTY_LIST;
                try {
                    actualList = (List<T>) o;
                }
                catch (ClassCastException e) {
                    return false;
                }
                Set<T> expectedSet = new HashSet<T>(expectedList);
                Set<T> actualSet = new HashSet<T>(actualList);
                return actualSet.equals(expectedSet);
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("should contain all and only elements of ").appendValue(expectedList);
            }
        };
    }
}

然后驗(yàn)證碼變成:

verify(mockClassB).sendEvent(argThat(MyMatchers.sameAsSet(expectedFileList)));

如果您改為創(chuàng)建模擬匹配器,則不需要 argThat,它基本上將 hamcrest 匹配器包裝在模擬匹配器中.

If you instead created a mockito matcher, you wouldn't need the argThat, which basically wraps a hamcrest matcher in a mockito matcher.

這會(huì)將排序或轉(zhuǎn)換的邏輯移出您的測(cè)試并使其可重用.

This moves the logic of sorting or converting to set out of your test and makes it reusable.

這篇關(guān)于模擬;使用列表調(diào)用驗(yàn)證方法,忽略列表中元素的順序的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to mock super reference (on super class)?(如何模擬超級(jí)參考(在超級(jí)類上)?)
Java mock database connection(Java 模擬數(shù)據(jù)庫(kù)連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設(shè)置為模擬對(duì)象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Is it possible to create a mock object that implements multiple interfaces with EasyMock?(是否可以使用 EasyMock 創(chuàng)建一個(gè)實(shí)現(xiàn)多個(gè)接口的模擬對(duì)象?)
主站蜘蛛池模板: 国产一区二区在线免费播放 | 四虎av电影 | 欧美日韩国产高清视频 | 久久久99国产精品免费 | 久久久久久久av麻豆果冻 | 国产精品久久久久久 | 欧美国产精品一区二区三区 | 四虎永久免费影院 | 久久精品影视 | 成人综合视频在线观看 | 久久这里有精品 | 日韩欧美国产一区二区 | 久久成人人人人精品欧 | av中文字幕在线播放 | 国产精品爱久久久久久久 | 亚洲69p | 精品国产欧美日韩不卡在线观看 | 精品无码久久久久久国产 | 欧美色人 | 97免费在线观看视频 | 男人的天堂avav | 国产亚洲一区二区三区在线 | 中国一级特黄真人毛片 | 99免费精品视频 | 日韩中文字幕在线 | 欧美bondage紧缚视频 | 亚洲www啪成人一区二区麻豆 | 国产欧美日韩精品一区 | 久久宗合色 | 97精品视频在线观看 | 午夜精品网站 | 久久久夜色精品亚洲 | 午夜三级网站 | 欧美日韩亚洲二区 | 亚洲国产aⅴ精品一区二区 免费观看av | 一区二区免费高清视频 | 国产精品一区二区三区四区 | 羞羞视频网站免费看 | 日韩欧美福利视频 | 日韩免费一区二区 | 欧美一区二区三区在线免费观看 |