Java learning notes - unit testing (JUnit framework)

Keywords: Java Junit unit testing

Before unit testing, you need to install the JUnit framework. Here you can see my previous blog
Record the problems encountered when installing the JUnit framework and the solutions

After installation, you can write the code of unit test

What is unit testing? Unit testing is to write test code for the smallest functional unit. The smallest functional unit of a java program is a method. Therefore, unit testing of a java program is a test for a single Java method.

First, let's write a method
This is the structure diagram of the project

The above mathDemo is a class written to be tested
The following mathDemoTest is a test class used to test mathDemo
Usually, we add Test after the class name to indicate that this is a Test class

The code of mathDemo class is used to find the area of a rectangle

package UnitDemo;

public class mathDemo {
    int area = 0;
    public int getArea(int a,int b){
        area = a*b;
        return area;
    }
}

Code for the mathDemoTest class

package UnitDemo;
import UnitDemo.mathDemo;
import org.junit.*;
import static org.junit.Assert.*;
public class mathDemoTest {
    mathDemo m = new mathDemo();

    @BeforeClass
    public static void setUpClass(){
        System.out.println("Execute before all test methods of the current class");
    }
    @AfterClass
    public static void tearDownClass(){
        System.out.println("Executes after all test methods in the current class");
    }

    @Before
    public void setUp(){
        this.m = new mathDemo();
        System.out.println("Execute before each test method");
    }
    @After
    public void tearDown(){
        this.m = null;
        System.out.println("Execute after each test method");
    }

    @Test
    public void testgetArea1(){
        assertEquals("This is error message 1",20,m.getArea(4,5));
    }
    @Test
    public void testgetArea2(){
        assertEquals("This is error message 2",12,m.getArea(3,4));
    }

}

@Test annotation method

  1. Each @ Test corresponds to a method, which will be recognized as a Test method
  2. There can be multiple @ tests in a Test class, but the Test method corresponding to each @ Test will only be executed once

Usually, we will use the assertEquals assertion statement in the @ Test test method to judge whether the method can run normally and output the desired results
assertEquals ("error message", expected result, actual result)
When the expected result is inconsistent with the actual result, an error message will be printed

This is the result when the assertions are correct and no error is reported

But if I modify the expected result so that the expected result is inconsistent with the actual result

@Test
    public void testgetArea(){
        assertEquals("This is error message 1",20,m.getArea(4,5)); //correct
        assertEquals("This is error message 2",16,m.getArea(3,4)); //error
    }


The error information given before will be printed here, and the detailed information will be printed to help us modify it

Here we need to talk about the meaning of these annotations:
@BeforeClass annotation method,

  1. It is the place where the whole test class starts to execute, and it is executed before all test methods of the current class.
  2. Is a static method
  3. It is used to initialize some cumbersome and time-consuming resources, such as creating a database.

@After class annotation method,

  1. This is the operation to be executed after the completion of the entire test class. It is executed after all test methods in the current class.
  2. Is a static method
  3. Used to clean up the resources initialized in the @ BeforeClass section

Note that these two are written in Junit4. In Junit5, these two are called @ BeforeAll and @ AfterAll respectively

@Before annotation method

  1. Execute before each test method.
  2. Is a non static method
  3. The object or property of the class used to initialize the class

@After annotation method

  1. Execute after each test method
  2. Is a non static method
  3. Used to clean up the objects or properties initialized in the @ Before part
    Note that these two are written in Junit4. In Junit5, these two are called @ BeforeEach and @ AfterEach respectively

@Before and @ After will be automatically executed before and After each Test method annotated by @ Test

	@Before
    public void setUp(){
        this.m = new mathDemo();
        System.out.println("Execute before each test method");
    }
    
    @Test
    public void testgetArea(){
        assertEquals("This is error message 1",20,m.getArea(4,5));
    }
    
    @After
    public void tearDown(){
        this.m = null;
        System.out.println("Execute after each test method");
    }

The specific calling sequence is like this. Each time you first call the @Before annotation method, initialize the object of the class, then run the test method, and then call the @After annotation method after the test method is finished, and clean up the space occupied by the initialization object before release.

Because of this, different @ Test test methods do not affect each other, because the objects of each @ Test test method are different

However, static variables initialized in @ BeforeClass and @ AfterClass will only be initialized and cleaned up once, which will affect all @ Test test methods

@Ignore annotation method
Methods annotated by @ Ignore will be ignored and will not be executed

	@Test
    public void testgetArea1(){
        assertEquals("This is error message 1",20,m.getArea(4,5));
    }
    
    @Ignore
    public void testgetArea2(){
        assertEquals("This is error message 2",16,m.getArea(3,4));
    }

The result is

Although the assertion in the second test method is wrong, there is no error, indicating that the test method has not been executed@ Before and @ After are only called once respectively.

Posted by warriors2003 on Sun, 10 Oct 2021 00:13:28 -0700