gpt4 book ai didi

java - 使用 JUnit 时跳过测试类中的方法

转载 作者:行者123 更新时间:2023-12-01 12:43:50 24 4
gpt4 key购买 nike

我有以下情况。我有课

A  {

public void setProps() {
// setting propertis

makeSomeWeirdConfigs();

//setting another properties

}

private void makeSomeWeirdConfigs() {
// making configs
}

public Props getProps() {
//getting properties
}
}

现在我想在 JUnit 测试用例的帮助下检查是否使用方法 setProps() 正确设置了属性,然后使用 getProps() 获取属性。

问题在于 setProps() 中间的这个方法 makeSomeWeirdConfigs() ,在没有适当环境的情况下执行时会导致很多异常,所以 props下面这个方法没有设置。

此外,此方法尝试做危险的事情,例如执行一些脚本等。所以我的问题是是否可以在 JUnit 测试用例中以某种方式跳过此方法 makeSomeWeirdConfigs() ,所以只有 setProps()getProps() 被执行。

原来的A类不应该改变。

谢谢

最佳答案

“原始类不应更改”-那么您正在寻找一些 mock 、字节码摆弄、AOP。

如果您使 makeSomeWeirdConfig 受到保护,您可以覆盖该方法并测试子类。与测试额外标志相同。

如果您可以将代码放入委托(delegate)人中,则可以更好地实现健全的代码。

class SomeWeirdConfig {
void makeSomeWeirdConfigs() { ... }
}

SomeWeirdConfig delegate = new SomeWeirdConfig();

public void initTest() { delegate = null; }

private void makeSomeWeirdConfigs() {
if (delegate != null) {
delegate.makeSomeWeirdConfigs();
}
}

然后测试可以禁用 setter 。

关于java - 使用 JUnit 时跳过测试类中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24860367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com