IDEA integrates SpringBoot to automatically generate unit tests and assert development

1. IDEA generation unit test process

Right-click in the interface file that needs to be tested-> go to-> test subject-> create test

 

 

 Then check the method that needs to be tested-> ok, just produce a test file in the same level package, and then add test logic:

import net.xdclass.xdvidio.domain.Video;
import net.xdclass.xdvidio.mapper.VideoMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.swing.*;

import java.util.List;

import static org.junit.Assert.*;

/**
 * @Author Pandas
 * @Date 2020/4/12 22:54
 * @Version 1.0
 * @Description 断言测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class VideoServiceTest {

    @Autowired
    private VideoService videoService;

    @Test
    public void findAll() {
        List<Video> list=videoService.findAll();
        assertNotNull(list);//断言
        for (Video video:list){
            System.out.println(video.getTitle());
        }
    }

    @Test
    public void findById() {
        Video video=videoService.findById(1);
        assertNotNull (video); 
    } 

    @Test 
    public  void update () { 
    } 

    @Test 
    public  void Delete () { 
    } 

    @Test 
    public  void Save () { 
    } 
}

2. Core notes:

Need to add the following two annotations on the test class

@RunWith (SpringRunner. Class ) // Tell java what running environment this class uses to run
@SpringBootTest

3. Assertion development

 Assertion keywords:

assert

, Is a new feature added after jdk1.4.

It is mainly used during code development and testing to judge certain key data. If this key data is not what your program expects, the program will issue a warning or exit.

Grammar rules:

assert expression;   // expression represents a boolean expression, if it is true, it will continue to run normally, if it is false, the program exits
assert expression1: expression2; // expression1 is a Boolean expression, expression2 is a basic type or Object type, if expression1 is true, the program ignores expression2 and continues to run; if expression1 is false, runs expression2, and then exits the program.

The Assert class in the org.junit package provides some commonly used assert methods, such as the method in the article:

assertNotNull (video); // If the object is not empty, it is normal; if it is empty, it is abnormal and the assertion fails

Its source code implementation:

    static public void assertNotNull(Object object) {
        assertNotNull(null, object);
    }

    static public void assertNotNull(String message, Object object) {
        assertTrue(message, object != null);
    }

    static public void assertTrue(String message, boolean condition) {
        if (!condition) {
            fail(message);
        }
    }
//
     static public void fail(String message) {
        if (message == null) {
            throw new AssertionError();
        }
        throw new AssertionError(message);
    }

    public AssertionError(Object detailMessage) {
        this(String.valueOf(detailMessage));
        if (detailMessage instanceof Throwable)
            initCause((Throwable) detailMessage);
    }

assertNotNull essentially assertTrue secondary packaging , and assertTrue in fact, with a message of if statements . . .

The source code is a good thing. See more and think more. Many methods are Russian dolls that are packaged many times.

 

 

Guess you like

Origin www.cnblogs.com/jwmdlm/p/12688885.html