0
0
JUnittesting~15 mins

Mutation testing concept (PIT) in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify mutation testing detects a simple mutation in Calculator add method
Preconditions (3)
Step 1: Run the CalculatorTest normally to verify it passes
Step 2: Modify the Calculator add method by changing '+' to '-' operator (simulate mutation)
Step 3: Run the CalculatorTest again
Step 4: Observe if the test fails due to mutation
✅ Expected Result: The test should fail after mutation, showing PIT detected the mutation
Automation Requirements - JUnit 5
Assertions Needed:
Assert that Calculator add method returns correct sum before mutation
Assert that test fails after mutation is introduced
Best Practices:
Use clear and simple test methods
Keep production and test code separate
Use PIT plugin or command line to run mutation tests
Do not hardcode mutation, rely on PIT tool to generate mutations
Automated Solution
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

class Calculator {
    public int add(int a, int b) {
        return a + b; // Original code
    }
}

public class CalculatorTest {

    @Test
    void testAdd() {
        Calculator calc = new Calculator();
        assertEquals(5, calc.add(2, 3), "2 + 3 should equal 5");
    }
}

/*
To perform mutation testing with PIT:
1. Run the CalculatorTest normally - it should pass.
2. Configure PIT in your build tool (Maven/Gradle) or IDE.
3. Run PIT mutation testing - it will automatically mutate the '+' operator to '-'.
4. PIT will run tests against mutated code.
5. If testAdd fails on mutated code, mutation is detected (killed).
*/

The Calculator class has a simple add method that sums two numbers.

The CalculatorTest class tests that add(2,3) returns 5.

Mutation testing tools like PIT automatically change code (e.g., '+' to '-') to check if tests catch errors.

If the test fails after mutation, it means the test is good and detects faults.

This example shows how to write a test that PIT can use to verify mutation detection.

Common Mistakes - 3 Pitfalls
Hardcoding mutations manually in production code
{'mistake': 'Not running mutation tests separately from normal tests', 'why_bad': "Mutation tests require special tool runs; running normal tests only won't detect mutations.", 'correct_approach': 'Use PIT plugin or command line to run mutation tests explicitly.'}
Writing tests that do not assert meaningful outcomes
Bonus Challenge

Now add tests for a subtract(int a, int b) method and verify mutation testing detects mutations in it

Show Hint