A bug caused by Boolean

Software Quality Assurance: What You Write Is What You Think|An Ali quality person’s perception of testing.

Recently, in the project test, a defect caused by Boolean was found. The main reason is that this problem is more interesting, and the code review process is not easy to find. The defect is essentially a contract problem.

Description of the problem

There is a call relationship between application A and application B, that is, B provides an interface for A to call. The contract between A-B has a parameter type that is Boolean. The business scenario of this parameter is that the value true is only passed in certain circumstances, and the default value false is passed in other business scenarios A.

Due to differences in previous communication, the development students on side B used Boolean instead of boolean type for this parameter when defining the contract. The strange thing is that the development did not find the problem due to the normal value transfer during the joint debugging process, but the problem was discovered after testing the abnormal scene after the test.

Before introducing specific defects, let’s understand the difference between Boolean and boolean.

The difference between Boolean and boolean

Java provides the Boolean class in the java.lang package. Boolean wraps a value of primitive type boolean in an object. An object of type Boolean contains a field of type Boolean. Additionally, this class provides useful methods such as converting boolean to string and string to boolean when dealing with boolean variables.

The Boolean class provides two constructors for creating Boolean objects.

Boolean b = newBoolean(boolean value);
Boolean b = newBoolean(String s);

A simple code example:

// Java program to demonstrate parseBoolean() method
public class Test
{
    public static void main(String[] args)
    {
        // parsing different Strings
        boolean b1 = Boolean. parseBoolean("True");
        boolean b2 = Boolean. parseBoolean("TruE");
        boolean b3 = Boolean. parseBoolean("False");
        boolean b4 = Boolean. parseBoolean("FALSE");
        boolean b5 = Boolean. parseBoolean("quality assurance");
         
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
        System.out.println(b4);
        System.out.println(b5);
    }
}
?
true
true
false
false
false

In the Java programming language, both Boolean and boolean are Boolean data. But there are some differences between them:

1. Boolean is a class while boolean is a primitive data type. Variables of type Boolean can be null before initialization whereas variables of type boolean cannot.

2. The Boolean type provides some methods, such as valueOf() and parseBoolean(), etc., in order to operate on it. However, the boolean type does not have these methods because it is a primitive type.

3. The Boolean type is usually used when a Boolean value needs to be stored in an object, while the boolean type is usually used for primitive data types such as local variables and arrays.

4. The two also differ in terms of autoboxing and unboxing. The Boolean type can be autoboxed and unboxed, while the boolean type can only be autoboxed and unboxed in Java 5 or later.

Defect restoration

The example is relatively simple, of course, the actual business logic is much more complicated.

Define a Payment class, which contains Boolean type attributes, used to distinguish whether the payment supports discounts.

import lombok. Data;
/**
 * @author qualityassurance
 * @version Payment.java, v 0.1 2023-03-19 15:52
 */
@Data
public class Payment {
    /**
     * Payment amount
     */
    private int amount;
    /**
     * Is discount available
     */
    private Boolean isDiscount;
}

Implement a function (API), the function is that if the transaction supports discount, the transaction price will be halved, otherwise the original price will be calculated.

public class DiscountFunction {
?
    public int calculateAmount(Payment payment){
        /**
         * If isDiscount=true, the price will be halved
         */
        if (payment. getIsDiscount(). booleanValue()){
            return payment. getAmount()/2;
        }
        return payment. getAmount();
    }
}

The test class implements two cases, namely the discounted scenario and the non-discounted scenario.

public class DiscountFunctionTest {
?
    /**
     * If isDiscount=true, the price will be halved
     */
    @Test
    public void isDiscountTest(){
        Payment payment = new Payment();
        payment.setAmount(1000);
        payment.setIsDiscount(true);
        DiscountFunction discountFunction = new DiscountFunction();
        int amount = discountFunction. calculateAmount(payment);
        Assert.assertEquals(payment.getAmount()/2, amount);
    }
?
    /**
     * If isDiscount=false, the price remains unchanged
     */
    @Test
    public void isNotDiscountTest(){
        Payment payment = new Payment();
        payment.setAmount(1000);
        DiscountFunction discountFunction = new DiscountFunction();
        int amount = discountFunction. calculateAmount(payment);
        Assert.assertEquals(payment.getAmount(), amount);
    }
}

operation result:

It can be seen that NPE is reported during the execution of the isNotDiscountTest use case. After analyzing the code, it is reported in the figure below, which perfectly reproduces the defects I found during the test.

Fix:

1. Change the property isDiscount type of the Payment class to a small Boolean boolean.

2. When using the large Boolean type, the judgment logic of the code should be added to judge Null.

Defect Analysis:

The main purpose of sharing this defect is to remind everyone that when they see the Boolean, they must pay attention to whether the development of the if judgment condition is considered comprehensively, and it is best to form such a conditioned reflex.

-END-

Scan the code below to pay attention to software quality assurance, learn and grow together with Quality Master, and make progress together, to be the most expensive Tester in the workplace!

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeHomepageOverview 108682 people are studying systematically