0
0
JUnittesting~5 mins

Mutation testing concept (PIT) in JUnit

Choose your learning style9 modes available
Introduction

Mutation testing helps check if your tests are strong by making small changes in your code and seeing if tests catch them.

You want to find out if your tests really check important parts of your code.
You want to improve weak tests that miss bugs.
You want to measure how good your test coverage really is.
You want to catch hidden mistakes before releasing software.
Syntax
JUnit
No direct code syntax; mutation testing is done by running PIT tool on your project which modifies your code and runs tests automatically.
PIT (Pitest) is a popular mutation testing tool for Java and JUnit.
You configure PIT in your build tool (like Maven or Gradle) and run it to see mutation reports.
Examples
This Maven command runs PIT mutation testing on your Java project.
JUnit
mvn org.pitest:pitest-maven:mutationCoverage
This Gradle command runs PIT mutation testing if PIT plugin is configured.
JUnit
gradle pitest
Sample Program

This simple Calculator class has an add method. The test checks if adding 2 and 3 equals 5.

PIT will change the '+' to '-' in add method and run the test. If the test fails, it means the test caught the mutation, so the test is strong.

JUnit
public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class CalculatorTest {
    @Test
    public void testAdd() {
        Calculator calc = new Calculator();
        assertEquals(5, calc.add(2, 3));
    }
}
OutputSuccess
Important Notes

Mutation testing can take longer than normal tests because it runs tests many times with small code changes.

Tests that do not fail on mutations are weak and should be improved.

Summary

Mutation testing checks how well your tests find bugs by changing code slightly.

PIT is a tool that automates mutation testing for Java projects using JUnit.

Strong tests fail when mutations are introduced, showing good test quality.