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

模擬 Java 枚舉以添加一個值來測試失敗案例

Mocking Java enum to add a value to test fail case(模擬 Java 枚舉以添加一個值來測試失敗案例)
本文介紹了模擬 Java 枚舉以添加一個值來測試失敗案例的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 enum 開關或多或少像這樣:

I have an enum switch more or less like this:

public static enum MyEnum {A, B}

public int foo(MyEnum value) {
    switch(value) {
        case(A): return calculateSomething();
        case(B): return calculateSomethingElse();
    }
    throw new IllegalArgumentException("Do not know how to handle " + value);
}

我希望測試涵蓋所有行,但由于代碼需要處理所有可能性,如果沒有相應的 switch 語句,我無法提供值.

and I'd like to have all the lines covered by the tests, but as the code is expected to deal with all possibilities, I cannot supply a value without its corresponding case statement in the switch.

擴展枚舉以添加額外的值是不可能的,并且僅僅模擬 equals 方法以返回 false 也不起作用,因為生成的字節碼使用窗簾后面的跳轉表去正確的情況......所以我認為也許可以使用 PowerMock 或其他東西來實現一些黑魔法.

Extending the enum to add an extra value is not possible, and just mocking the equals method to return false won't work either because the bytecode generated uses a jump table behind the curtains to go to the proper case... So I've thought that maybe some black magic could be achieved with PowerMock or something.

謝謝!

編輯:

由于我擁有枚舉,我認為我可以只為值添加一個方法,從而完全避免切換問題;但我要留下這個問題,因為它仍然很有趣.

As I own the enumeration, I've thought that I could just add a method to the values and thus avoid the switch issue completely; but I'm leaving the question as it's still interesting.

推薦答案

如果您可以使用 Maven 作為構建系統,您可以使用更簡單的方法.只需在測試類路徑中使用附加常量定義相同的枚舉.

If you can use Maven as your build system, you can use a much simpler approach. Just define the same enum with an additional constant in your test classpath.

假設您在源目錄 (src/main/java) 下聲明了枚舉,如下所示:

Let's say you have your enum declared under the sources directory (src/main/java) like this:

package my.package;

public enum MyEnum {
    A,
    B
}

現在您在測試源目錄 (src/test/java) 中聲明完全相同的枚舉,如下所示:

Now you declare the exact same enum in the test sources directory (src/test/java) like this:

package my.package

public enum MyEnum {
    A,
    B,
    C
}

測試使用重載"枚舉查看 testclass 路徑,您可以使用C"枚舉常量測試您的代碼.你應該會看到你的 IllegalArgumentException.

The tests see the testclass path with the "overloaded" enum and you can test your code with the "C" enum constant. You should see your IllegalArgumentException then.

在 windows 下使用 maven 3.5.2、AdoptOpenJDK 11.0.3 和 IntelliJ IDEA 2019.3.1 測試

Tested under windows with maven 3.5.2, AdoptOpenJDK 11.0.3 and IntelliJ IDEA 2019.3.1

這篇關于模擬 Java 枚舉以添加一個值來測試失敗案例的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 亚洲一区久久 | 精品久久久久久 | 欧美一区二区三区 | 成人日韩精品 | 午夜影院在线观看免费 | 亚洲男人天堂 | 亚洲三区在线观看 | 免费观看一区二区三区毛片 | 日韩在线成人 | www.四虎.com | 久久久一区二区 | 盗摄精品av一区二区三区 | 精品少妇一区二区三区在线播放 | 国产精品亚洲视频 | 日韩成人影院在线观看 | 91精品国产91久久久久久吃药 | 美女久久久 | 一区二区三区中文字幕 | 在线观看黄色电影 | 黄色在线免费观看视频 | 日本黄色一级视频 | 一区视频在线 | 久久久久久精 | a级片www| 国产成人精品一区二区三区网站观看 | 亚洲国产精品91 | 日韩在线免费 | 国产免费一区二区三区 | 日韩电影免费在线观看中文字幕 | 亚洲欧美日韩久久久 | 玖玖色在线视频 | 亚洲综合久久久 | 国产精品久久国产精品 | 午夜专区 | 日本 欧美 三级 高清 视频 | 亚洲在线日韩 | 欧洲尺码日本国产精品 | 国产日韩一区二区 | 日韩中文字幕在线 | 欧美精品久久久久 | 视频一区二区在线 |