简体   繁体   English

解决分支机构覆盖声纳问题

[英]Fixing Branch Coverage Sonar issue

I am trying to run sonar and i take a sample of my code a sampleClass to fix the branch coverage issue: 我正在尝试运行声纳,并且将我的代码示例sampleClass修复了分支覆盖问题:

The issue was 117 more branches need to be covered by unit tests to reach the minimum threshold of 65.0% branch coverage. 问题是,单元测试还需要覆盖117个分支,才能达到65.0%分支覆盖率的最低阈值。

I was trying to let my test cases cover many branches in the sample class. 我试图让我的测试用例涵盖示例类中的许多分支。

but the number 117 cannot be changed after many trials. 但是数字117经过多次试验后无法更改。 What i have to do to fix this issue? 我必须做什么才能解决此问题?

You need to add more tests. 您需要添加更多测试。 For instance: 例如:

@Test
public void testThis(){
  if ( getBooleanA() || getBooleanB()){
    assertTrue(getBooleanA() != getBooleanB());
    }
  else{
    assertTrue(getBooleanA() == getBooleanB());
  }
}

here, you need to provide tests for the next cases: 1. boolean A and B are both false 2. boolean A and B are both true 3. boolean A is true, and boolean B is false 4. boolean A is false, and boolean B is true 在这里,您需要为以下情况提供测试:1.布尔A和B均为假2.布尔A和B都为真3.布尔A为真,布尔B为假4.布尔A为假,并且布尔B是真的

if you miss one of those tests, there's a branch you haven't covered. 如果您错过其中一项测试,则有一个分支尚未涵盖。

EDIT: it is clear (or it should be), that the assert in the else block is pointless, but I just added it, in case it didn't return a boolean, but an int, to show how easily it is to have a new branch that needs covering. 编辑:很明显(或者应该是),else块中的断言是没有意义的,但是我只是添加了它,以防它没有返回布尔值,而是一个int值,以显示它具有多容易需要覆盖的新分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM