top of page

Coffee and Tips Newsletter

Inscreva-se na nossa newsletter semanal

Nos vemos em breve!

How to create Mutation tests with Pitest

Writer: JP
JP
Feb 7, 2021
3 min read




Pitest is a tool that provides state-of-the-art mutation testing. It was written to run on Java applications or on applications that run on top of a JVM.


How it works?


Pitest works by generating mutant codes that will make changes to the bytecodes in the source code, changing logic, removing and even changing method return values. Thus, it's possible to evaluate the code in a more complete way, looking for failures and making the code reliable.


Basic example of mutation


See the code below:

if(name == ""){
   return "Empty name";
}else{
    return name;
}

Running Pitest on the code above, it's able to modify the bytecodes by changing the code above to:

if(name != ""){
   return name;
}else{
    return "Empty name";
}

Pitest enables you identifying possible points of failure and guide you to create tests based on these points.


Hands on time


Maven

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.pitest</groupId>
        <artifactId>pitest</artifactId>
        <version>1.6.2</version>
    </dependency>
</dependencies>

Let's create a simple class representing a bank account called BankAccount and to make it simple we'll implement the logic inside the entity class itself.


In this class, we have a method called userValid that checks if the user is valid.

package com.bank.entity;

public class BankAccount {

    public String bankName;
    public String user;
    public Double balance;

    public Boolean userValid(BankAccount bankAccount){
        if(bankAccount.user != ""){
            return true;
        }else{
            return false;
        }
    }
}

Without creating any test classes, run this command via Maven:

mvn org.pitest:pitest-maven:mutationCoverage

See that a report was generated in the console with the mutations created based on the conditions that must be tested.



Looking at the console above, we have 3 mutations or point of failures, which means we need to eliminate these mutations from our code.


Pitest provides another way to validate these mutations through a report generated by an HTML page. You can access these generated reports through /target/pit-reports/ folder


In the image below we can see a report that summarizes the coverage percentage. Note that we have 0% of coverage.

In the BankAccount.java.html file shows details about uncovered code parts.


Note that in the image above there is a list of Active mutators that are mutations based on the code we created.


Removing these mutations


Based on the reports, let's create some tests to reach out 100% of coverage. First of all, we need to create a class test. For this example, we're going to create a class test called BankAccountTest.java. To do this, there's an attention point.


You'll have to create this class with the same package's name used for the BankAccount.java. Thus, create inside src/test/java a package called com.bank.entity and finally BankAccountTest.java.


package com.bank.entity;

import org.junit.Assert;
import org.junit.Test;

public class BankAccountTest {


    @Test
    public void userValid_NegateConditionalsMutator_Test(){

        BankAccount bankAccount = new BankAccount(
                "Chase","Jonas", 2000.00);

        Boolean actual = bankAccount.userValid(bankAccount);
        Boolean expected = true;
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void userValid_BooleanFalseReturnValsMutator_Test(){

        BankAccount bankAccount = new BankAccount(
                "Chase","", 2000.00);

        Boolean actual = bankAccount.userValid(bankAccount);
        Boolean expected = false;
        Assert.assertEquals(expected, actual);
    }

}

We have created only 2 test method for our coverage:


1.userValid_NegateConditionalsMutator_Test(): Covers filled-in usernames. The constructor passed the "Chase" argument representing the username so that the method's condition is validated. And finally validates the condition of the method to be returned as TRUE.


To this case, we already eliminated two mutations according to statistic report.

  1. BooleanTrueReturnValsMutator

  2. NegateConditionalsMutator

2. userValid_BooleanFalseReturnValsMutator_Test(): Validates the possibility of the method returning the Boolean value FALSE.


Rerun the Maven command below:

mvn org.pitest:pitest-maven:mutationCoverage

Application console


Note that for every mutation generated, 1 was eliminated. Covering 100% coverage in tests.


References: https://pitest.org


Books to study and read


If you want to learn more about and reach a high level of knowledge, I strongly recommend reading the following book(s):


Unit Testing Principles, Practices, and Patterns: Effective Testing Styles, Patterns, and Reliable Automation for Unit Testing, Mocking, and Integration Testing with Examples in C# is a book that covers Unit Testing Principles, Patterns and Practices teaches you to design and write tests that target key areas of your code including the domain model. In this clearly written guide, you learn to develop professional-quality tests and test suites and integrate testing throughout the application life cycle.














Mastering Unit Testing Using Mockito and JUnit is a book that covers JUnit practices using one of the most famous testing libraries called Mockito. This book teaches how to create and maintain automated unit tests using advanced features of JUnit with the Mockito framework, continuous integration practices (famous CI) using market tools like Jenkins along with one of the largest dependency managers in Java projects, Maven. For you who are starting in this world, it is an excellent choice.














Well that’s it, I hope you enjoyed it!




18 Comments


bentieshamarsh.al.2l11.1
6 days ago

cổng game rikvip lúc đầu mình cũng hơi ngại, sợ kiểu trang rối rắm với chữ nghĩa đầy đầu, vào rồi lại phải mò mẫm mãi. Nhưng lướt qua một chút thì thấy họ làm giao diện khá thẳng thắn, cái mình cần nhìn là nhận ra ngay, không phải đoán từng nút. Phần “Đăng Ký” để khá nổi trên trang nên người mới như mình đỡ mất thời gian tìm chỗ mở tài khoản, bấm vào là đi đúng hướng luôn. Mấy bài viết cũng trình bày gọn, tiêu đề lớn, nhìn kiểu “Tải Rikvip” là biết nội dung nói gì chứ không vòng vo. Nói chung vẫn còn giữ chút nghi ngờ thôi. Nhưng ít nhất bố cục…

Like

johnniwill.ia.ms4069
Sep 06

https://qq88pro.vip/ mình vào thử đúng vài phút để xem bố cục thế nào. Trang tải lên nhanh, nhìn phát là biết chỗ nào là menu để bấm qua lại. Mình để ý phần nội dung được chia thành mấy khối dạng ô nên mắt dễ bắt, không cần đọc nhiều vẫn nắm được ý chính. Kéo xuống thì thông tin xếp theo hàng cột khá thẳng, kiểu trình bày gọn gàng nên lướt cũng đỡ mỏi. Mình không đi sâu hay tìm gì cụ thể, chỉ thử chuyển giữa vài mục xem có bị lag không. Cảm giác thao tác ổn, nút bấm phản hồi nhanh, chữ với nền cũng dễ nhìn. Nói chung cách họ sắp xếp các khung…

Like

laurasanms311989
Sep 06

https://hi88.uno/ nói thật là lúc mới lướt mình cũng hơi lăn tăn, kiểu nhìn na ná vài trang giải trí trước đây nên không biết có dễ dùng hay lại rối. Nhưng kéo xuống một lúc thì thấy họ chia nội dung thành từng khối khá rành mạch, nên dù không đọc kỹ vẫn nhận ra đoạn nào đang nói về gì, không bị dồn chữ một cục. Mình chỉ xem nhanh thôi, không có thử sâu. Có cái mình thích là mấy phần thông tin dạng bảng/cột nhìn gọn, số với tiêu đề tách ra rõ nên mắt đỡ phải căng, và menu cũng nằm chỗ dễ thấy nên chuyển qua lại không phải mò lâu. Nói chung vẫn…

Like

cap.rir.us07.4
Sep 05

link qs88 thấy chủ đề trên trang cứ nói kiểu “peak 2026” nghe hơi kêu quá nên mình tò mò bấm vào xem thử cho biết, lỡ đâu lại rối rắm như nhiều trang khác; mà vào rồi thì ngạc nhiên nhẹ, họ chia nội dung thành mấy khối rõ ràng nên mình lướt một hơi vẫn nắm được mạch chính, không cần ngồi đọc kỹ từng dòng; mình cũng hay nghi ngờ mấy câu giới thiệu “quốc tế” các kiểu, nhưng phần nói QS88 thành lập năm 2020 và nhắc chuyện đầu tư AI viết khá thẳng, ít vòng vo nên đọc đỡ khó chịu; nói chung mình không rành cá cược gì đâu, chỉ xem bố cục và…

Like

anyadoug.hert16.7
Sep 03

cà khịa là kiểu trang mình mở lên là thấy ngay chỗ xem trực tiếp, khỏi phải mò mẫm qua mấy lớp linh tinh nên lúc đang vội tìm trận cho kịp giờ cũng đỡ cáu, mình thử trên điện thoại với máy tính đều vào nhanh và cái mình thích nhất là nó không ép tạo tài khoản hay đăng nhập gì cả, cứ bấm là xem thôi; đang xem mà đường truyền hơi chập thì có mấy luồng server để đổi qua lại nên cũng cứu được vài pha, chứ đứng hình hoài là tụt mood liền. Chữ nghĩa trên trang nhìn rõ. Các khối nội dung cũng chia ra gọn gàng. Tiêu đề và mục “trực tiếp…

Like
bottom of page