(SpringBoot)(二)SpringBoot集成Junit单元测试

SpringBoot工程的启动方式有两种,一种是通过SpringBootApplication的启动类启动工程,第二种就是集成Junit进行单元测试。接下来,本菜鸟将带大家进行SpringBoot整合Junit

1、添加Junit的起步依赖

 <!--添加测试junit的起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

2、编写测试类进行测试

import com.netposa.springbootmybatis.domain.User;
import com.netposa.springbootmybatis.mapper.UserMapper;
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 java.util.List;

@SpringBootTest(classes = SpringbootmybatisApplication.class)
@RunWith(SpringRunner.class)
public class test {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void test(){
        List<User> list = userMapper.getAllUser();
        System.out.println(list);
    }
}

从整体上看SpringBoot集成Junit还是非常非常简便的,更甚至如果使用的是IDEA创建的项目,根本就无须自己再导入依赖,在创建项目时,IDEA就已经帮我们添加了Junit的依赖。

但此处有几个需要注意的点:
1.@SpringBootTest后边跟的是我们配置的启动类的反射对象

2.@RunWith后边跟的就是一个运行器,后边可以是SpringRunner类的反射,也可以是SpringJUnit4ClassRunner类的反射

3.千万不要忘记在测试方法上边加上@Test注解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值