java - 无效问题 : boolean replaced return of integer sized value with (x == 0 ? 1 : 0) → SURVIVED

标签 java identity mutation-testing

更新: 自从发布这个问题后,突变覆盖失败就消失了,可能是由于坑中的错误修复?

所以这个题现在是无效的。我不知道在这种情况下该怎么办。删除问题?留在这里?

我正在使用 pit 进行突变测试。一个非常简单的函数中有一个僵尸:

public boolean isAuthenticated() {
    return true;
}

它经过测试:

 assertEquals(true, isAuthenticated());

但我收到错误 replaced return of integer sized value with (x == 0 ? 1 : 0) → SURVIVED

我曾尝试使用 System.identityHashCode() 检查身份,但这没有帮助。

有什么办法可以杀死这只丧尸? 如果不是,我如何关闭此特定的 boolean 值检查或此特定的代码行?

最佳答案

我认为错误并没有揭示问题的根源,在我的情况下(很可能在任何情况下)这样做是不可能的。

在我的例子中,我有一个对象列表,其中包含一个返回谓词的函数。因此,我使用流来遍历所有这些对象,并将其中一个与谓词匹配的对象与另一个对象作为候选对象进行过滤。所以就我的代码而言:

  public List<ParkingSlot> getFreeParkingSlots(List<DepartmentParkingRule> ruleList, Employee candidate){
      return 
          ruleList.stream().
          .filter(j -> j.getPredicate().test(candidate)) // <-- here appeared the error *replaced return of integer sized value with (x == 0 ? 1 : 0) → SURVIVED*
          .map(t -> t.getFreeParkingSlots(candidate))
          .findFirst()
          .orElseThrow(new NotMatchedException(candidate));
  }

我预计(通过分析/设计)一名候选员工只匹配一个部门规则。我认为使用 .findFirst() 很合适。 PIT 否认。通过将通常不匹配的案例(部门)的谓词结果突变为“突变匹配”,可能会有多个匹配项,而不是一个匹配项。

所以我修改了我的代码以在有多个匹配项的情况下抛出异常。在此更改之后,突变体被杀死。所以我现在的功能是:

  public List<ParkingSlot> getFreeParkingSlots(List<DepartmentParkingRule>  ruleList,
                                                 Employee candidate){

      Optional<List<ParkingSlot>> parkingSlots = ruleList.stream().
           .filter(j -> j.getPredicate().test(candidate)) 
           .map(t -> t.getFreeParkingSlots(candidate))
           .reduce((anElement, otherElement) -> 
                    throw new DuplicateException());//reduce is executed when a possible false positive duplicate rule match

      if (parkingSlots.isPresent() && CollectionUtils.isNotEmpty(parkingSlots.get())){
         return parkingSlots.get();
      }else {
         throw new NotMatchedException(candidate);
      }
  }

因此,错误 replaced return of integer sized value with (x == 0 ? 1 : 0) → SURVIVED 表明突变发生了,最重要的是这个突变没有导致我的测试失败.

下次我看到这个错误时,我会从整体上看对我的测试的影响,而不是特定的 boolean 函数。

请注意,上面的代码不是真实的,但对于展示如何解释错误和修改源代码的操作很有用。改变测试可能有助于杀死那个突变体,但这取决于每个案例。

希望对大家有帮助。

关于java - 无效问题 : boolean replaced return of integer sized value with (x == 0 ? 1 : 0) → SURVIVED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58567056/

相关文章:

Java Hadoop MapReduce 多值

haskell - Clojure 中的 Identity 函数与 Haskell 中的 id 函数具有相同的用法和用途吗?

forms - 通过 Web 表单使用 PGP 或 SSL 签署 HTML 文档

c# - C# 中的变异测试工具

c# - 差一错误和突变测试

java - 突变测试 - 以 for-each 循环为条件否定?

java - 在线程中使用forEach循环引用的线程安全性

java - 如何将双引号附加到 JPA 实体类表名中的表名?

java - Android,说话失败: TTS engine connection not fully set up

c# - 在表中插入数据之前是否可以获取 Id (IDENTITY) 的新值?