springboot之测试

2020-04-07

1.引入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

2.在 test 目录下创建测试类

20191022145437111.png

3.为测试类增加 @RunWith(SpringRunner.class) 和 @SpringBootTest 注解

4.使用 @Autowired 注入待测试类的对象

5.编写测试方法,增加 @Test 注解

package com.laozhang.dao;

import com.laozhang.entity.User;
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;

/**
 * @author zhangfan
 * @date 2019/10/21
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDaoTest {

    @Autowired
    private UserDao userDao;

    @Test
    public void testFindByAccount(){
        User zhangsan = userDao.findByAccount("zhangsan");

        System.out.println(zhangsan);
    }
}



标题:springboot之测试
作者:张范
地址:http://misterzhang.top/articles/2020/04/07/1586248580306.html