[英]skip methods from tested class when using JUnit
我有以下情況。 我有一堂課
A {
public void setProps() {
// setting propertis
makeSomeWeirdConfigs();
//setting another properties
}
private void makeSomeWeirdConfigs() {
// making configs
}
public Props getProps() {
//getting properties
}
}
現在,我想借助JUnit測試用例檢查是否通過使用方法setProps()
正確設置了屬性,然后使用getProps()
獲得了屬性。
問題是這樣的方法makeSomeWeirdConfigs()
中的中間setProps()
其中,當沒有適當的環境中執行造成大量異常的,所以下面這種方法道具未設置。
另外,此方法還會嘗試執行危險的操作,例如執行一些腳本等。因此,我的問題是,是否可以在JUnit測試用例中以某種方式跳過此方法makeSomeWeirdConfigs()
,因此僅執行setProps()
和getProps()
。
原始的A類不應更改。
謝謝
“不應更改原始類”-然后,您正在尋找一些嘲笑,弄亂字節碼,AOP的東西。
如果使makeSomeWeirdConfig
受保護,則可以覆蓋該方法並測試子類。 與測試額外標志相同。
如果您可以將代碼委托給聲音代碼,則更好。
class SomeWeirdConfig {
void makeSomeWeirdConfigs() { ... }
}
SomeWeirdConfig delegate = new SomeWeirdConfig();
public void initTest() { delegate = null; }
private void makeSomeWeirdConfigs() {
if (delegate != null) {
delegate.makeSomeWeirdConfigs();
}
}
然后測試可以禁用設置器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.