7.1 Spring Boot单元测试

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16855077/article/details/84973919

1.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.cloudtech</groupId>
	<artifactId>01-springboot-hello</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath />
		<!-- lookup parent from repository -->
	</parent>
	
	<properties>
	    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<!-- junit jar包 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		
		<!-- mybatis启动器  -->
	    <dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		
		<!-- mysql数据库驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- druid数据库连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.9</version>
		</dependency>
		
		<!-- 增加thymeleaf坐标  -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		
		<!-- 热部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
		
		<!--通用Mapper -->
		<dependency>
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper-spring-boot-starter</artifactId>
			<version>2.0.3</version>
		</dependency>
		<!--pageHelper分页 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.5</version>
		</dependency>
	</dependencies>
</project>

后面的演示的demo用到springboot整合mybatils+通用mapper,不了解的朋友可以去查看4.1章节的博客

2. 编写junit代码

package com.cloudtech.test;

import java.util.List;

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.SpringJUnit4ClassRunner;

import com.cloudtech.App2;
import com.cloudtech.dao.RoleMapper;
import com.cloudtech.entity.Role;

/**
 * spring 测试类
 * @RunWith:启动器
 * SpringJUnit4ClassRunner  让junit和spring环境进行整合
 * @SpringBootTest  1.当前类为spring的测试类   2加载spring boot启动类,启动spring boots
 * 
* @ClassName: RoleTest  
* @Description:   
* @author wude  
* @date 2018年12月12日  
*
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={App2.class})
public class RoleTest {
	@Autowired
	RoleMapper roleMapper;
	
	@Test
	public void testAddRole(){
		List<Role> select = roleMapper.select(null);
		for (Role role : select) {
			System.out.println("id:"+role.getId()+",name:"+role.getName());
		}
	}
}

3 测试结果

猜你喜欢

转载自blog.csdn.net/qq_16855077/article/details/84973919