我正在使用坑进行突变测试。一个非常简单的功能有一个僵尸:

public boolean isAuthenticated() {
    return true;
}


经过测试:

 assertEquals(true, isAuthenticated());


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

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

有什么办法杀死这个僵尸吗?
如果没有,如何关闭布尔值的特定检查或代码的特定行?

最佳答案

我认为该错误并没有揭示问题的根源,就我而言(很可能无论如何),这样做是不可能的。

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

  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表示发生了突变,最重要的是,该突变并未导致我的测试失败。

下次看到此错误时,我将着眼于对测试整体的影响,而不是特定的布尔函数。

只是要注意,上面的代码不是真实的,而是对显示如何解释错误以及修改源代码所采取的操作很有用。更改测试可能有助于杀死该突变体,但这取决于每种情况。

希望它可以帮助某人。

07-24 09:38
查看更多