【问题标题】:Showing Not Covered by Test if method throw exception junit mockito如果方法抛出异常junit mockito,则显示未覆盖测试
【发布时间】:2021-02-09 02:22:19
【问题描述】:
implementation class have two method :
test method call by test case and inside test() i call test2() which is throw exception, now I want 
to 
cover this method by test case but in sonar it showing not covered by test , so how i can covered this 
method:

 [service implementation class][1]

public void test()throws SystemException {
    LOGGER.info("Testing exception");
    test2(); //not covered by test
    LOGGER.info("Testing exception 2");//not covered by test
    System.out.print("hi");//not covered by test
}

public void test2() throws SystemException {
    LOGGER.info("Testing exception");
    throw new SystemException();
}

测试用例:这是调用 service.test1() 方法的测试,但在代码覆盖率中显示不是 被测试覆盖:

@Test(expected = SystemException.class)
public void test1() throws SystemException  {
   service.test();      
}

【问题讨论】:

    标签: unit-testing mockito sonarqube junit4


    【解决方案1】:

    您非常不清楚您的问题到底是什么。当你提到“这个方法”或“它”时,你必须清楚它的确切含义。

    关于您现有的代码,如果您想知道如何“覆盖”方法“test”的最后两行,则无法覆盖它们。事实上,根本不可能执行这些行。如果方法“test2”抛出该异常,它将退出方法“test2”以及方法“test”,而不执行方法“test”的其余部分。

    为了更清楚您的要求,您可以尝试将您的方法名称更改为稍微不同,并明确引用这些方法名称。

    【讨论】:

      猜你喜欢
      • 2013-02-15
      • 2021-02-08
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      相关资源
      最近更新 更多