Search code examples
javacode-coveragemutationmutation-testingpitest

Pitest - Mutation Test failure - negated conditional → SURVIVED


I added a conditional operator in my code like:

String test;//assigned with some value
result = test != null ? test : "";

This fails in mutation testing with the reason of negated conditional → SURVIVED

I added few unit test cases to assign the value with "" , null, "value" like

asserThat(testValue.equals(""));

But nothing is giving the solution for this.

Can I get help from anyone about the mutation coverage for this specific flow?


Solution

  • assertThat(testValue.equals(""));
    

    Is not a functioning assertion. Assuming you are using AssertJ, this should be.

    assertThat(testValue).isEqualTo("");
    

    It is not entirely clear what your code and tests look like from the small snippets you have posted. But the negated conditional is killed in the following code with the supplied test.

    public class Foo {
        public static String foo(String test) {
            String result = test != null ? test : "";
            return result;
        }
    }
    
    class FooTest {
        @Test
        void convertsNullToEmptyString() {
            assertEquals("", Foo.foo(null));
        }
    
        @Test
        void returnsNonNullValues() {
            assertEquals("hello", Foo.foo("hello"));
        }
    }