山东大学暑期项目实训——云主机服务比价与预测系统(八)
学习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、启动测试