Link Search Menu Expand Document

Spring Support

Requires pitest 1.15.0 or above. Minimum Java version 11.

Provided by the arcmutate-spring plugin.

Maven Central

Background

Teams using pitest with spring face two issues

  1. Tests using spring test extensions may fail to detect mutants
  2. Important functionality is determined by annotations instead of Java code

The arcmutate spring plugin addresses both these issues.

Spring Environment Support

The plugin automatically clears the spring context after each test, ensuring that mutated code is executed. This is similar to adding @DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) to each spring test class, but caches of values that spring holds outside the context are also cleared.

Tests which rely on state persisting between test methods may fail as a result of the context being cleared. These tests will need to be excluded from the analysis, or rewritten so they can be run in isolation.

It is important to note that, even with the spring plugin installed, pitest only supports mutation testing with unit tests that run all code and tests within the same jvm. It cannot be used with integration tests that launch a web server.

Performance Note

We recommend that your test suite uses Spring test extensions only sparingly. Where possible, plain unit tests with no knowledge of the spring framework should be preferred. Tests that must launch all (or part) of spring execute far more slowly than plain unit tests, and increase feedback time as a project grows. Mutation testing amplifies this problem.

When it is not possible to write a plain unit test, we recommend using Spring’s test slices.

Annotation Mutations

The plugin currently provides several new mutation operators. They can be enabled individually, or all mutators can be enabled by using the mutator group “SPRING”.

REMOVE_VALIDATION

Removes the following annotations from classes, fields, methods and method parameters.

  • jakarta.validation.constraints.Min
  • jakarta.validation.constraints.Max
  • jakarta.validation.constraints.NotEmpty
  • jakarta.validation.constraints.Pattern
  • javax.validation.constraints.Min
  • javax.validation.constraints.Max
  • javax.validation.constraints.NotEmpty
  • javax.validation.constraints.Pattern

REMOVE_SECURITY

Removes the following annotations from classes, fields, methods and method parameters.

  • org.springframework.security.access.prepost.PreAuthorize
  • org.springframework.security.access.prepost.PostAuthorize
  • jakarta.annotation.security.RolesAllowed
  • org.springframework.security.access.prepost.PreFilter
  • org.springframework.security.access.prepost.PostFilter
  • org.springframework.security.access.annotation.Secured

REMOVE_RESPONSE

Removes the following annotations from classes, fields, methods and method parameters.

  • org.springframework.web.bind.annotation.ResponseStatus
  • org.springframework.web.bind.annotation.ResponseBody

Installation

To you use the plugin you must first acquire a licence.

The licence file must be named arcmutate-licence.txt and placed at the root of the project.

The plugin itself should be placed on the classpath of the pitest plugin.

E.g for maven

<plugin>
  <groupId>org.pitest</groupId>
  <artifactId>pitest-maven</artifactId>
  <version>1.16.0</version>
    <configuration>
        <mutators>
          <mutator>DEFAULTS</mutator>
          <mutator>SPRING</mutator>
        </mutators>
    </configuration>
    <dependencies>
      <dependency>
        <groupId>com.arcmutate</groupId>
        <artifactId>arcmutate-spring</artifactId>
        <version>1.0.0</version>
      </dependency>
    </dependencies>
</plugin>

Or for gradle

dependencies {
  pitest 'com.arcmutate:arcmutate-spring:1.0.0'
}

Individual operators or groups should then be enabled using the standard pitest configuration options.