Testing framework in SpringBoot

1. When creating a springboot project, the test framework will be automatically created
insert image description here
2. Right-click in the class that needs to be tested and click Generate, select Test
insert image description here
3. Add the @SpringBootTest annotation to the test class to declare that
the current class is in the Spring Boot container run in

4. Build the unit test code in the method

@SpringBootTest
@Transactional() // 单元测试不污染业务数据
class UserControllerTest {
    
    
    @Autowired
    private UserController userController;
    @Test
    void getAll() {
    
    
    }

    @Test
    void add() {
    
    
        UserInfo userInfo = new UserInfo();
        userInfo.setName("Spring");
        userInfo.setPassword("spring");
        // 调用需要进行单元测试的方法
        int result = userController.add(userInfo);
        Assertions.assertEquals(1,result);
    }
}

Guess you like

Origin blog.csdn.net/qq_39537400/article/details/124407304