1. 程式人生 > 實用技巧 >SpringBoot單元測試

SpringBoot單元測試

引入依賴

  引入spring-boot-starter-test依賴,其中包含了junit,所以不需要額外引入junit。

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
<!--	<version>2.1.16.RELEASE</version>-->
	<scope>test</scope>
</dependency>

測試  

  在測試類上面增加@SprintBootTest註解和@RunWith註解(@RunWith由junit提供),並且在@RunWith中指定SpringRunner即可,如下所示:

package cn.ganlixin.business;


import cn.ganlixin.model.bo.UserInfoBO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserServiceTest {

    @Resource
    private UserService userService;

    @Test
    public void testSelectAll() {
        List<UserInfoBO> allUser = userService.getAllUser();
    }
}

  

  原文連結:https://www.cnblogs.com/-beyond/p/13675358.html