当使用一个带有自动注入字段的方法时,变异应该被杀死,但实际没有被杀死。

13

我有以下内容:

public class UnsetProperty extends Command {

    @Resource
    private SetProperty setProperty;

    public String parse(String[] args) {
        if (args.length != 4) {
            throw new RuntimeException("Incorrect number of arguments. Expected 4. Got " + args.length);
        }
        String publisher = args[0];
        String version = args[1];
        String mode = args[2];
        String property = args[3];

        /*
         * Unsetting a property is done by changing the current property to null.
         * Technically, the old property doesn't get changed, a new one is inserted with
         * a higher revision number, and it becomes the canonical one.
        */
        setProperty.setProperty(publisher, version, mode, property, null, false);
        return "";
    }
}

并进行以下测试:

public class UnsetPropertyTest extends CommandTest {
    @Configuration
    public static class Config {

        @Bean(name = "mockSetProperty")
        public SetProperty getSetProperty() {
            return mock(SetProperty.class);
        }

        @Bean
        public UnsetProperty getUnsetProperty() {
            return new UnsetProperty();
        }

    }

    @Resource
    @InjectMocks
    private UnsetProperty unsetProperty;

    @Resource(name = "mockSetProperty")
    private SetProperty setProperty;

    // ... SNIP ...

    @Test
    public void testCallsSetPropertyWithCorrectParameters() throws SQLException, TaboolaException {
        final String[] args = new String[]{"publisher", "version", "mode", "property"};
        final String output = unsetProperty.parse(args);
        verify(setProperty).setProperty("publisher", "version", "mode", "property", null, false);
        // The above line should have killed the mutation!
        verifyNoMoreInteractions(setProperty);
        assertThat(output).isEqualTo("");
    }
}
测试通过,如预期所料。当我运行它通过PIT时,我得到以下结果。
33   1. removed call to my/package/SetProperty::setProperty → SURVIVED

在类代码中,第33行被突出显示。

测试内容如下:

  • my.package.UnsetPropertyTest.testCallsSetPropertyWithCorrectParameters(my.package.UnsetPropertyTest) (32 毫秒)
  • my.package.UnsetPropertyTest.testUnsetThrowsForIncorrectNumberOfParameters(my.package.UnsetPropertyTest) (3 毫秒)

现在:

  • 当我更改测试调用参数(args)时,测试失败。这是预期的。
  • 当我更改断言(verify(setProperty).setProperty(...))参数时,测试失败。这是预期的。
  • 当我手动注释掉第一个代码块中突出显示的函数调用时,测试失败。

为什么变异会生存下来?

我正在使用Java 8、Mockito 1.9.5和PIT 1.1.4。


@henry 是的。我已经更新了我的问题,并列出了测试。 - Madara's Ghost
其他类中的变异体是否按预期被杀死了? - henry
你能把它精简成一个最小的项目,以重现这个问题吗? - henry
@henry 这已经是非常基础的了。你缺少什么? - Madara's Ghost
只需要一个可运行的东西,包括依赖项、构建文件等,并确保你所看到的东西在你的机器上能够复现。 - henry
显示剩余4条评论
1个回答

1
多年过去了,但似乎没有人提到(Spring)@Resource和(Mockito)@InjectMocks是互斥的解决方案。您有多个生成的UnsetProperty子类在运行中,因此PIT对发生的情况感到困惑。

我已经没有测试你的答案的能力了(4.5年确实很长时间),但希望其他遇到同样问题的人可以尝试。 - Madara's Ghost

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,