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.