山东大学暑期项目实训——云主机服务比价与预测系统

山东大学暑期项目实训——云主机服务比价与预测系统(八)

学习Spring Boot整合Junit

SpringBoot为什么要整合Junit?

SpringBoot整合了Junit后,在写了Mapper接口后,可直接通过Junit进行测试,不用再写Controller层,不用启动引导类之后通过页面的形式一层一层的调用。

在SpringBoot整合Mybatis基础上,添加如下3步即可整合Junit并进行测试:

1、配置Junit的起步配置(pom.xml)

<!--SpringBoot集成Junit-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

2、编写测试类

package com.itheima;
import ch.qos.logback.core.net.SyslogOutputStream;
import com.itheima.domain.User;
import com.itheima.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;

//用哪个类来运行
@RunWith(SpringRunner.class)
//classes配置的是SpringBoot的引导类
@SpringBootTest(classes = SpringbootMybatisApplication.class)
public class MybatisTest {
  //注入自定义的要测试的Mapper接口
  @Autowired
  private UserMapper userMapper;

  //用于声明这是一个测试方法
  @Test
  public void test(){
    List<User> users = userMapper.queryUserList();
    System.out.println(users);
  }
}

3、启动测试

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值