gpt4 book ai didi

java - 在私有(private)方法内编写 switch 语句的测试用例

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

我在私有(private)方法中有一个 switch case 语句。 I don't think we need to do unit testing for the private method.但我的代码覆盖率工具(EclEmma)显示“错过了 4 个分支中的 1 个”。 switch 语句上带有黄色菱形。所以我的问题是:如何针对这种特殊情况编写测试?

代码片段

public void parentMethod() {
....
childMethod(someList);
....
}

private void childMethod(List<Integer> someList) {
for(Integer var : someList) {
switch(var){ ..... }
}
}

最佳答案

因此,您可以尝试使用反射,如下所示:

MyClass myClass = new MyClass();  
List<Integer> input = Arrays.asList(1, 2, 3);

Method method = MyClass.class.getDeclaredMethod("childMethod", List.class);
method.setAccessible(true);
method.invoke(myClass, input);
....

有关测试私有(private)方法或一般不进行测试的方法的良好链接:

http://saturnboy.com/2010/11/testing-private-methods-in-java/

http://www.artima.com/suiterunner/privateP.html

关于java - 在私有(private)方法内编写 switch 语句的测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26065549/

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