私は次のものを持っています:
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 を使用しています。