Why can't Junit unit tests have return values?

This problem originated from what our teacher said in the last class when a boy in our class asked him what he thought. I thought it was quite interesting when I first heard this. When I used unit testing before, I seemed to subconsciously write its return value as void. I usually conduct simple tests and have never thought about calling another unit test in a certain unit test. I wrote his idea into a simple case, as shown below:

package JunitTest;

import org.junit.Test;

public class ExampleTest {
    
    
    @Test
    public int add(){
    
    //单元测试1
        int a=10;
        int b=20;
        return a+b;
    }
    @Test
    public void result(){
    
    //单元测试2
        System.out.println("结果为:"+add());//使用单元测试1的返回值
    }
}

The program error is as follows:

Insert image description here

The reason is: juint unit testing framework stipulates , 测试方法必须是void类型的, 不能有返回值. This is because the purpose of unit testing is to verify that the behavior and results of the method are as expected , not to obtain the return value of the method . Whether the test passes or fails is determined by assertions. If the assertion fails, the test fails, indicating that the behavior or results of the method are not as expected. Therefore, unit test methods do not need to return values.

Guess you like

Origin blog.csdn.net/m0_64365419/article/details/133417605