首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >排除PITest中的某些代码行

排除PITest中的某些代码行
EN

Stack Overflow用户
提问于 2017-05-04 12:46:01
回答 1查看 843关注 0票数 3

我正在使用优秀的PITest框架。我想知道在PITest中是否存在类似于声纳"// NOSONAR“的东西,即某些行被排除在PITest覆盖范围之外(所以报告中没有红色)?我知道方法和类可以被排除在外,我只是在寻找行级更细粒度的东西。

我拥有的用例如下:

代码语言:javascript
复制
public enum FinancialStatementUserType {
CONSUMER, BUSINESS, RETAILER;

}

代码语言:javascript
复制
    public static RecipientType toRecipientType(FinancialStatementUserType userType) {
        Assert.notNull(userType, "userType is null");

        switch (userType) {
           case BUSINESS:
           case CONSUMER:
               return RecipientType.CustomerPerson;
           case RETAILER:
            return RecipientType.Seller;

        default:
            throw new IllegalStateException(String.format("No RecipientType for financial statement user type: %s", userType));
    }
}

我遇到的问题是“默认”子句不可访问,因为所有枚举当前都包含在switch语句中。我们添加'detault‘语句的原因(除了这是一个良好的实践),是因为枚举在将来会被扩展。

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-05 09:02:51

这是不可能排除代码的每行级别上的最坏-它工作在已编译的字节码,因此没有访问标记和注释在代码中,因为这些是在编译时丢失。

您可以进行的最细粒度的排除是在方法级别。

对于这里突出显示的特定情况,一个可能的选项是更改您的编码样式。

如果RecipientType类型与FinancialStatementUserType有很强的关联,则可以通过使关系显化来确保逻辑不会中断新FinancialStatementUserType的添加。

代码语言:javascript
复制
enum FinancialStatementUserType {
  CONSUMER(RecipientType.CustomerPerson), 
  BUSINESS(RecipientType.CustomerPerson), 
  RETAILER(RecipientType.Seller);

  private final RecipientType recipientType;  

  FinancialStatementUserType(String recipientType) {
    this.recipientType = recipientType;
  }

  RecipientType recipientType() {
    return recipientType;
  }

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43783301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档