第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 JMockit 中正確模擬私有 ExecutorService 的提交方法

如何在 JMockit 中正確模擬私有 ExecutorService 的提交方法

江戶川亂折騰 2023-06-28 15:31:38
我有一個包含私有 ExecutorService 實例的類。在類中,我有一個方法運行提交方法并捕獲 RejectedExecutionException。但是,我在模擬 ExecutorService 實例以引發(fā)異常以便我可以完成測試覆蓋率時遇到了麻煩。我正在使用 JMockit 1.45。我已經(jīng)瀏覽過 JMockit 教程和其他網(wǎng)站;無論我使用@Mocked、@Capturing,還是創(chuàng)建一個新的假類,它似乎都不起作用。// Implemented Class:public class TaskRegister {    private ExecutorService executor;    public TaskRegister() {        this.executor = Executors.newFixedThreadPool(5);    }    public void executeTask(Runnable task) {        try {            this.executor.submit(task);        } catch (RejectedExecutionException e) {            System.out.println(e.getMessage);        }    }}// Unit Test Class:public class TestTaskRegister {    @Tested    private TaskRegister tested;    private static int counter;    @Test // this works    public void runNormalTask() throws InterruptedException {        counter = 0;        Runnable mockTask = new Runnable() {            counter++;        }        tested.executeTask(mockTask);        Thread.sleep(100); // Allow executor to finish other thread.        assertEquals(1, counter);    }    @Test // this doesn't work, will have missing invocation error.    public void throwsError (@Capturing ExecutorService executor) throws InterruptedException {        counter = 0;        // somehow the tested class still runs the actual executor         // and not the mocked one.        new Expectations() {{             executor.submit((Runnable) any);             result = new RejectedExecutionException();        }};        Runnable mockTask = new Runnable() {            // some task        }        tested.executeTask(mockTask);        Thread.sleep(100);        assertEquals(0, counter);    }}我希望 @Capturing 攔截真正的執(zhí)行程序?qū)崿F(xiàn)并在調(diào)用 executor.submit 時拋出異常,但它沒有這樣做。
查看完整描述

2 回答

?
萬千封印

TA貢獻1891條經(jīng)驗 獲得超3個贊

模擬的@Capturing成本可能很高,并且在某些情況下可能會導(dǎo)致意外結(jié)果,因此(目前)所有java.*類都被排除在外。所以,java.util.concurrent.ThreadPoolExecutor在這個測試中不會被嘲笑(它可以與@Mocked)。

實際上,RejectedExecutionException異常永遠不會發(fā)生(至少不會發(fā)生ThreadPoolExecutor- 可能只會發(fā)生ForkJoinPool)。所以,這個測試不值得付出努力。事實上,由于該異常是一個,RuntimeException您可以簡單地catch完全刪除該塊。

這是(濫用)模擬庫發(fā)生的壞事之一:人們有時使用它們來測試不可能的情況,因此編寫無用的測試。


查看完整回答
反對 回復(fù) 2023-06-28
?
慕娘9325324

TA貢獻1783條經(jīng)驗 獲得超4個贊

您可以使用Executorin 參數(shù)創(chuàng)建一個新的構(gòu)造函數(shù)。

因此,您的類將更加可配置(因為目前您只能使用一種執(zhí)行器),并且您將能夠通過使用模擬執(zhí)行器手動實例化該類來測試該類。


public class TaskRegister {


    private ExecutorService executor;


    public TaskRegister(ExecutorService executor) {

        this.executor = executor;

    }


    public TaskRegister() {

        this(Executors.newFixedThreadPool(5));

    }


    public void executeTask(Runnable task) {

        try {

            this.executor.submit(task);

        } catch (RejectedExecutionException e) {

            System.out.println(e.getMessage);

        }

    }

}


查看完整回答
反對 回復(fù) 2023-06-28
  • 2 回答
  • 0 關(guān)注
  • 227 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號