代码覆盖率未达到类声明 [英] Code coverage does not reach class declaration

查看:77
本文介绍了代码覆盖率未达到类声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法像这样覆盖类的类声明的代码?

Is there any way to get the code coverage to cover the class declaration of a class like so?

public class MyClass{

    public static void foo(int bar){
        System.out.println("The Number is: "+bar);
    }
}

我可以通过JUnit测试轻松地使用foo方法,但是MyClass声明保持红色.这是因为类本身没有构造函数吗?如果是这样,有什么办法可以覆盖那段代码,而无需更改类本身的代码?

I can easily hit the foo method with JUnit testing, but the MyClass declaration stays red. Is this because the class itself has no constructor? And if so, is there any way to cover that bit of code, without changing the code of the class itself?

谢谢

推荐答案

这可能取决于您的特定环境.但是我只是检查了Eclipse/EclEmma,然后看到了您描述的行为.

This may depend on your specific environment. But I just checked Eclipse/EclEmma and see the behavior you describe.

请记住,类确实有一个构造函数-这是默认的构造函数.如果您进行的测试仅调用new MyClass(),则红色标记似乎消失了.

Remember, the class does have a constructor - it's the default constructor. If you make a test that simply calls new MyClass(), it looks like the red mark goes away.

但是-仅具有静态方法的类的首选方法是将该类标记为final并创建一个私有构造函数.当然,如果您创建一个私有构造函数,那么它将在代码覆盖率中显示为红色-因为您无法调用私有构造函数!

BUT - the preferred approach for a class with only static methods is to mark the class as final and create a private constructor. Of course if you create a private constructor, that will show up as red in the code coverage - because you can't call a private constructor!

最后,请记住,代码覆盖率是一种工具.我不会对查看器中的红色标记感到厌烦.

Finally though, remember that code coverage is a tool. I wouldn't get all worked up about a red mark in the viewer.

这篇关于代码覆盖率未达到类声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆