PiTest“改变条件边界突变幸存”无缘无故?

Pow*_*tat 7 pitest junit5 java-11

我有一个带有 JUnit 5 测试的小型 Java 11 示例,结果是:

改变条件边界 ? 幸存下来

主要类:

public final class CheckerUtils
 {
  private CheckerUtils()
   {
    super();
   }


  public static int checkPort(final int port)
   {
    if (port < 0)
     {
      throw new IndexOutOfBoundsException("Port number out of range!");
     }
    return port;
   }

 }
Run Code Online (Sandbox Code Playgroud)

测试类:

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

import de.powerstat.security.CheckerUtils;


public final class CheckerUtilsTests
 {
  @Test
  public void checkPortOk()
   {
    final int port = 1023;
    final int resultPort = CheckerUtils.checkPort(port);
    assertEquals(port, resultPort, "Port not as expected!");
   }


  @Test
  public void checkPortNegative1()
   {
    final int port = -1;
    assertThrows(IndexOutOfBoundsException.class, () ->
     {
      CheckerUtils.checkPort(port);
     }
    );
   }


  @Test
  public void checkPortNegative2()
   {
    final int port = -1;
    int resultPort = 0;
    try
     {
      resultPort = CheckerUtils.checkPort(port);
     }
    catch (final IndexOutOfBoundsException e)
     {
      // ignore
     }
    assertEquals(0, resultPort, "Port is not 0");
   }

 }
Run Code Online (Sandbox Code Playgroud)

从我的角度来看,突变不应该存在,因为:

  1. checkPortOk() 是非负合法值的正常路径
  2. checkPortNegative1() 是当注意发生变异并抛出异常时负值的路径。
  3. checkPortNegative2():当没有任何变化时,抛出异常并且 resultPort 仍然为 0 - 所以这里的断言是可以的
  4. checkPortNegative2(): 当 < 0 变异为 < -1 或更低时,则不会抛出异常,因此 resultPort 将变为 -1 并且断言将失败(变异被杀死)
  5. checkPortNegative2():当 < 0 突变为 < 1 或更高时,与 3 下相同。

所以我的问题是我在这里遗漏了什么还是它是 pitest (1.4.9) 中的一个错误?

解决方案

正如@henry 的 statet,添加以下测试解决了这个问题:

@Test
public void checkPortOk2()
 {
  final int port = 0;
  final int resultPort = CheckerUtils.checkPort(port);
  assertEquals(port, resultPort, "Port not as expected!");
 }
Run Code Online (Sandbox Code Playgroud)

hen*_*nry 11

条件边界突变会发生变异

if (port < 0)
Run Code Online (Sandbox Code Playgroud)

if (port <= 0)
Run Code Online (Sandbox Code Playgroud)

由于没有任何测试提供 0 的输入,因此它们无法区分突变体和未突变的程序,突变体将存活下来。

添加描述端口为 0 时预期行为的测试用例应该会杀死突变体。