2014-07-21 23 views
0

我有以下情况。我有一个类使用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级不应该改变。

感谢

+1

您可以使用Powermock嘲笑私有方法 – schlagi123

+0

可以提供例如 – user3546762

+0

这里你可以找到一个例子:https://code.google.com/p/powermock/wiki/MockPrivate – schlagi123

回答

1

“原班不应该改变” - 那么你正在寻找的东西嘲讽,字节码摆弄,AOP。

如果您制作了makeSomeWeirdConfig保护,您可以重写该方法并测试一个子类。测试一个额外的标志也是一样。

如果你可以把代码放在委托人甚至更好的声音代码。

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

SomeWeirdConfig delegate = new SomeWeirdConfig(); 

public void initTest() { delegate = null; } 

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

然后测试可以禁用setter。