spring boot的测试和spring mvc的测试类似。在spring boot中,每次新建项目的时候,都会自动加上spring-boot-starter-test的依赖,这样我们就没必要测试的时候再添加额外的jar包。

spring boot还会创建一个当前项目的测试类,位于src/test/java的包下:
      新建一个spring boot项目

依赖jpa,web,pom.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.jack</groupId><artifactId>springboot19test</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot19test</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.8.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-data-jpa</artifactId></dependency><!--mysql连接驱动--><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

application.yml配置如下:

server:port: 9090
spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/jackusername: rootpassword: rootjpa:hibernate:#hibernate\u63D0\u4F9B\u4E86\u6839\u636E\u5B9E\u4F53\u7C7B\u81EA\u52A8\u7EF4\u62A4\u6570\u636E\u5E93\u8868\u7ED3\u6784\u7684\u529F\u80FD\uFF0C\u53EF\u901A\u8FC7spring.jpa.hibernate.ddl-auto\u6765\u914D\u7F6E\uFF0C\u6709\u4E0B\u5217\u53EF\u9009\u9879#create\uFF1A\u542F\u52A8\u65F6\u5220\u9664\u4E0A\u4E00\u6B21\u751F\u6210\u7684\u8868\uFF0C\u5E76\u6839\u636E\u5B9E\u4F53\u7C7B\u751F\u6210\u8868\uFF0C\u8868\u4E2D\u6570\u636E\u4F1A\u88AB\u6E05\u7A7A#create-drop\uFF1A\u542F\u52A8\u65F6\u6839\u636E\u5B9E\u4F53\u7C7B\u751F\u6210\u8868\uFF0CsessionFacotry\u5173\u95ED\u65F6\u8868\u4F1A\u88AB\u5220\u9664#update\uFF1A\u542F\u52A8\u65F6\u6839\u636E\u5B9E\u4F53\u7C7B\u751F\u6210\u8868\uFF0C\u5F53\u5B9E\u4F53\u7C7B\u5C5E\u6027\u53D8\u52A8\u7684\u65F6\u5019\uFF0C\u8868\u7ED3\u6784\u4E5F\u4F1A\u66F4\u65B0\uFF0C\u5728\u521D\u671F\u5F00\u53D1\u9636\u6BB5\u4F7F\u7528\u6B64\u9879#validate\uFF1B\u542F\u52A8\u65F6\u9A8C\u8BC1\u5B9E\u4F53\u7C7B\u548C\u6570\u636E\u8868\u662F\u5426\u4E00\u81F4\uFF0C\u5728\u6211\u4EEC\u6570\u636E\u7ED3\u6784\u7A33\u5B9A\u65F6\u91C7\u7528\u6B64\u9009\u9879#none\uFF1A\u4E0D\u91C7\u53D6\u4EFB\u4F55\u63AA\u65BDddl-auto: update#show-sql\u7528\u6765\u8BBE\u7F6Ehibernate\u64CD\u4F5C\u7684\u65F6\u5019\u5728\u63A7\u5236\u53F0\u663E\u793A\u5176\u771F\u5B9E\u7684sql\u8BED\u53E5show-sql: trueproperties:hibernate:dialect: org.hibernate.dialect.MySQL5Dialectjackson:serialization:#\u8BA9\u63A7\u5236\u5668\u8F93\u51FA\u7684json\u5B57\u7B26\u4E32\u683C\u5F0F\u66F4\u7F8E\u89C2indent_output: true

实体类代码:

package com.jack.springboot19test.entity;import javax.persistence.*;/*** create by jack 2017/10/3*/
//@Entity注解指明这是一个和数据库表映射的实体类
@Entity
public class Person {/*** 主键id* @Id注解指明这个属性映射为数据库的主键* @GeneratedValue定义主键生成的方式,下面采用的是mysql的自增属性*/@Id@GeneratedValue(strategy= GenerationType.AUTO)private Integer id;/*** 姓名*/private String name;/*** 年龄*/private Integer age;/*** 地址*/private String address;public Person() {super();}public Person(Integer id, String name, Integer age, String address) {super();this.id = id;this.name = name;this.age = age;this.address = address;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}
}

数据访问:

package com.jack.springboot19test.dao;import com.jack.springboot19test.entity.Person;
import org.springframework.data.jpa.repository.JpaRepository;/*** create by jack 2017/11/5*/
public interface PersonRepository extends JpaRepository<Person,Integer>{
}

控制器:

package com.jack.springboot19test.controller;import com.jack.springboot19test.dao.PersonRepository;
import com.jack.springboot19test.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** create by jack 2017/11/5*/
@RestController
@RequestMapping("person")
public class PersonController {@Autowiredprivate PersonRepository personRepository;@RequestMapping(value = "findall",method = RequestMethod.GET,produces = {MediaType.APPLICATION_JSON_VALUE})public List<Person> findAll(){return personRepository.findAll();}}

测试用例:

package com.jack.springboot19test;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jack.springboot19test.dao.PersonRepository;
import com.jack.springboot19test.entity.Person;
import org.junit.Assert;
import org.junit.Before;
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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;//14.版本之前用的是SpringJUnit4ClassRunner.class
@RunWith(SpringRunner.class)
//1.4版本之前用的是//@SpringApplicationConfiguration(classes = Application.class)
@SpringBootTest(classes = Springboot19testApplication.class)
@WebAppConfiguration
@Transactional
public class Springboot19testApplicationTests {@Autowiredprivate PersonRepository personRepository;private MockMvc mockMvc;@AutowiredWebApplicationContext webApplicationContext;String expectedJson;/*@Testpublic void contextLoads() {}*/@Beforepublic void setUp()throws JsonProcessingException{/*Person p1 = new Person();p1.setName("jack6");Person p2 = new Person();p2.setName("jack7");personRepository.save(p1);personRepository.save(p2);*/expectedJson = Obj2Json(personRepository.findAll());mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();}public String Obj2Json(Object obj)throws JsonProcessingException {ObjectMapper mapper = new ObjectMapper();return mapper.writeValueAsString(obj);}@Testpublic void testPersonController()throws Exception{String uri="/person/findall";MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON_VALUE)).andReturn();int status = result.getResponse().getStatus();String content = result.getResponse().getContentAsString();System.out.println("content : "+content);Assert.assertEquals("错误,正确的返回值为200",200,status);Assert.assertEquals("错误,返回值和预期返回值不一致",expectedJson,content);}}

代码地址:https://github.com/wj903829182/SpringCloudTwo/tree/master/springboot19test

SpringBoot33-springboot开发部署与测试-spring boot测试相关推荐

  1. 那些在《JavaEE开发的颠覆者 Spring Boot实战》中遇到的坑,,。(一)

    一.一开始下了一本PDF书,影印版,看一般的字还是看的清,但是看到代码部分的话,还是会有模糊,尤其是一些配置的时候,后来是在不行就去网上搜了一下,找到一个网易云阅读上有网页版的<JavaEE开发 ...

  2. JavaEE开发的颠覆者 Spring Boot实战

    网站 更多书籍点击进入>> CiCi岛 下载 电子版仅供预览及学习交流使用,下载后请24小时内删除,支持正版,喜欢的请购买正版书籍 电子书下载(皮皮云盘-点击"普通下载" ...

  3. eclipse创建springboot项目_创建一个 Spring Boot 项目,你会几种方法?

    我最早是 2016 年底开始写 Spring Boot 相关的博客,当时使用的版本还是 1.4.x ,文章发表在 CSDN 上,阅读量最大的一篇有 42W+,如下图: 2017 年由于种种原因,就没有 ...

  4. 使用JUnit 5进行Spring Boot测试

    JUnit 5 (JUnit Jupiter)已经存在了相当长的一段时间,并且配备了许多功能. 但令人意外JUnit 5它不是一个默认的测试库相关,当涉及到春节开机测试入门:它仍然是JUnit 4.1 ...

  5. ssm如何支持热部署_最新Spring Boot实战文档推荐:项目搭建+配置+SSM整合

    在Spring Boot项目中,正常来说是不存在XML配置,这是因为Spring Boot不推荐使用XML,注意,排不支持,Spring Boot推荐开发者使用Java配置来搭建框架, Spring ...

  6. 电商生鲜网站开发(一)——Spring Boot项目开发准备

    本系列内容完成Spring Boot框架的电商生鲜网站开发的完整案例,前后端分离开发的案例,先开发后端接口后开发前端,最后部署等待. Spring Boot项目开发准备 文章目录 Spring Boo ...

  7. spring boot测试_测试Spring Boot有条件的合理方式

    spring boot测试 如果您或多或少有经验的Spring Boot用户,那么很幸运,在某些时候您可能需要遇到必须有条件地注入特定bean或配置的情况 . 它的机制是很好理解的 ,但有时这样的测试 ...

  8. 使用Testcontainers和PostgreSQL,MySQL或MariaDB的Spring Boot测试

    Testcontainers是一个Java库,可轻松将Docker容器集成到JUnit测试中. 在Containerized World中 ,将测试配置与嵌入式数据库和服务复杂化几乎没有意义. 而是使 ...

  9. 在Spring Boot测试中使用Testcontainer进行数据库集成测试

    在此博客文章中,我想演示如何在Spring Boot测试中集成Testcontainer以便与数据库一起运行集成测试. 我没有使用Testcontainers的Spring Boot模块. 如何与他们 ...

最新文章

  1. Linux centos 6.7防火墙打开MySQL 3306端口
  2. [SCOI2013]多项式的运算
  3. 远见卓识,领导力在于把握企业潮流
  4. 【ArcGIS遇上Python】ArcGIS python计算长时间序列多个栅格数据的平均值
  5. java 线程工厂_Java并发编程:Java的四种线程池的使用,以及自定义线程工厂
  6. 封禁3年,微信重磅功能回归!
  7. 通用的《求职信》范文模板
  8. 刀片服务器接显示器,认识刀片服务器
  9. matlab信号处理小波变换
  10. 软件测试过程的四个阶段(单元测试、集成测试、系统测试、验收测试)
  11. 迷你迅雷,IE下载加速补丁(转)
  12. 肥姐沈殿霞离世追悼会时间待定 为女儿留下上亿遗产
  13. 解密:斐讯N1为何火了?分享全套N1救砖指南!值得收藏
  14. 高考还能够改变普通人的命运么?数据分析来告诉你!!
  15. 使用eNSP搭建两个交换机通过trunk实现相同vlan互联
  16. std::accumulate的用法
  17. linux define路径,linux架设BT Tracker服务器小记
  18. jQuery和CSS制作霓虹灯文字效果
  19. OGG-误删DDL触发器ggs_ddl_trigger_before恢复
  20. 擎标带您了解DCMM贯标带来了哪些成效

热门文章

  1. 两步搞定暴风影音的文字广告!
  2. mysql数据库实验报告一
  3. 由“16亿美元终极锦鲤”引发的金钱观思考 文末有彩蛋
  4. Google、华为、今日头条都在用的绩效管理OKR是什么? | 推荐收藏
  5. ORA-12514:TNS:listener does not currently know of service requested in connect descriptor
  6. git添加文件并且提交
  7. 数据库排行榜|当 DB-Engines 遇见墨天轮国产数据库排行
  8. 小程序自定义带凹的tabbar
  9. 等级保护2.0达标要求及变化
  10. js 实现trim()的两种方法