This tutorial covers integrating PIT Mutation Testing into a Spring Boot project and configuring Jenkins for automated reporting.
In this tutorial, you’ll learn how to integrate PIT Mutation Testing into a Spring Boot project, execute mutation tests, and publish the HTML report in Jenkins. We’ll walk through:
Configuring the Maven POM
Updating the Jenkinsfile
Installing and configuring the PIT plugin in Jenkins
@RestControllerpublic class NumericController { @GetMapping("/") public String welcome() { return "Kubernetes DevSecOps"; } @GetMapping("/compare/{value}") public String compareToFifty(@PathVariable int value) { if (value > 50) { return "Greater than 50"; } else { return "Smaller than or equal to 50"; } }}
Existing tests only checked status codes, allowing some mutations to survive.Update your tests to assert response content:
Copy
Ask AI
@RunWith(SpringRunner.class)@SpringBootTest@AutoConfigureMockMvcpublic class NumericApplicationTests { @Autowired private MockMvc mockMvc; @Test public void smallerThanOrEqualToFiftyMessage() throws Exception { mockMvc.perform(get("/compare/50")) .andExpect(status().isOk()) .andExpect(content().string("Smaller than or equal to 50")); } @Test public void greaterThanFiftyMessage() throws Exception { mockMvc.perform(get("/compare/51")) .andExpect(status().isOk()) .andExpect(content().string("Greater than 50")); } @Test public void welcomeMessage() throws Exception { mockMvc.perform(get("/")) .andExpect(status().isOk()) .andExpect(content().string("Kubernetes DevSecOps")); }}
Open index.html in a browser to explore the complete mutation report.Congratulations! You’ve successfully integrated PIT mutation testing, configured Jenkins for automated reporting, identified weak spots, and enhanced your tests for robust coverage.