博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Javaweb——Spring Boot 系列(20)Spring Boot Test
阅读量:3932 次
发布时间:2019-05-23

本文共 7583 字,大约阅读时间需要 25 分钟。

Spring Boot Test

一、Spring Boot 对测试的支持

  • Spring Boot 的测试与 Spring MVC 很类似。
  • Spring Boot 提供了 spring-boot-starter-test 依赖用于进行测试相关的支持,并且每次新建 Spring Boot 项目都会默认添加,同时会在 src/test/java 目录下新建一个 项目名+Test 的测试类。
  • 接下来用一个简单的 Spring Boot 进行测试。

1、新建项目

  • 新建一个 Spring Boot 项目,依赖选择为 JPA、Web 和 hsqldb。
  • 其中 hsqldb 为内存数据库。
  • 项目的 POM 文件如下:
    4.0.0
    org.springframework.boot
    spring-boot-starter-parent
    1.3.0.M4
    com.pyc
    springtest
    0.0.1-SNAPSHOT
    springtest
    jar
    A project of spring test
    1.8
    UTF-8
    org.springframework.boot
    spring-boot-starter-data-jpa
    org.springframework.boot
    spring-boot-starter-web
    org.hsqldb
    hsqldb
    runtime
    org.springframework.boot
    spring-boot-starter-test
    test
    org.springframework.boot
    spring-boot-maven-plugin
    spring-snapshots
    Spring Snapshots
    https://repo.spring.io/snapshot
    true
    spring-milestones
    Spring Milestones
    https://repo.spring.io/milestone
    false
    spring-snapshots
    Spring Snapshots
    https://repo.spring.io/snapshot
    true
    spring-milestones
    Spring Milestones
    https://repo.spring.io/milestone
    false
  • application.properties 文件不用编辑。

2、待测试的业务代码

  • 编辑一个业务用于测试。

2.1、实体类

  • 首先是实体类,代码如下:
    package com.pyc.springtest.domain;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Person {
    @Id @GeneratedValue private Long id; private String name; public Person() {
    super(); } public Person(String name) {
    super(); this.name = name; } public void setId(Long id) {
    this.id = id; } public Long getId() {
    return id; } public void setName(String name) {
    this.name = name; } public String getName() {
    return name; }}

2.2、实体类 Repository

  • 新建一个 PersonRepository,代码如下:
    package com.pyc.springtest.dao;import com.pyc.springtest.domain.Person;import org.springframework.data.jpa.repository.JpaRepository;public interface PersonRepository extends JpaRepository
    {
    }
  • 因为是用于测试,因此便不再添加额外的方法,继承默认的方法即可。

2.3、控制器

  • MVC 种控制器是必不可少的,控制器代码如下:
    package com.pyc.springtest.web;import com.pyc.springtest.dao.PersonRepository;import com.pyc.springtest.domain.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;@RestController@RequestMapping("/person")public class PersonController {
    @Autowired PersonRepository personRepository; @RequestMapping(method = RequestMethod.GET, produces = {
    MediaType.APPLICATION_JSON_VALUE}) public List
    findAll(){
    return personRepository.findAll(); }}

3、测试用例

  • 打开项目默认创建的测试类,修改并编辑代码如下:
    package com.pyc.springtest;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.pyc.springtest.dao.PersonRepository;import com.pyc.springtest.domain.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.SpringApplicationConfiguration;import org.springframework.http.MediaType;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;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;@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = SpringtestApplication.class)@WebAppConfiguration@Transactionalclass SpringtestApplicationTests {
    @Autowired PersonRepository personRepository; MockMvc mvc; @Autowired WebApplicationContext webApplicationContext; String expectedJson; protected String Obj2Json(Object obj) throws JsonProcessingException{
    ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(obj); } @Before public void setUp() throws JsonProcessingException{
    Person p1 = new Person("pyc"); Person p2 = new Person("ycy"); personRepository.save(p1); personRepository.save(p2); expectedJson = Obj2Json(personRepository.findAll()); mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test public void testPersonController() throws Exception{
    String uri = "/person"; MvcResult result = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)) .andReturn(); int status = result.getResponse().getStatus(); String content = result.getResponse().getContentAsString(); Assert.assertEquals("错误,正确的返回值为 200", 200, status); Assert.assertEquals("错误,返回值和预期返回值不一致", expectedJson, content); }}
  • 对几个注解说明一下:
  • @SpringApplicationConfiguration 替代 @ContextConfiguration 来配置 Spring Boot 的 Application Context。
  • @Transactional 注解可保证每次测试后的数据将会被回滚。
  • @Befor 注解是 Junit 提供的,可用于在测试开始前进行一些初始化的工作。

4、执行测试

  • 在项目根路径打开 Terminal,像我一样使用 IDEA 的直接可在编辑窗口下面的 Terminal 窗口直接打开。
  • 在 Terminal 窗口输入 mvn clean package -Dmaven.test.skip=true 命令回车,等待程序的自动执行,再无错误的情况下,Terminal 窗口的结果界面如下:
    在这里插入图片描述
  • 并且在 Project 窗口的 target 目录下会生成一个项目的 JAR 包,如下:
    在这里插入图片描述

转载地址:http://jlqgn.baihongyu.com/

你可能感兴趣的文章
sql练习--查找所有员工的last_name和first_name以及对应部门编号dept_no,也包括展示没有分配具体部门的员工
查看>>
sql练习--查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工
查看>>
Transformer-based Object Re-Identification论文解读
查看>>
Android BLE开发
查看>>
Java内部类详解
查看>>
Android开发常见面试题类型
查看>>
2017美团校招安卓岗
查看>>
YUV基础知识《转载》
查看>>
C语言动态申请内存
查看>>
cmake万能模板
查看>>
让你不再害怕指针——C指针详解
查看>>
十张图解释机器学习的基本概念
查看>>
红黑树:自平衡的二叉查找树
查看>>
回收站功能在 Linux 中的实现
查看>>
数据包头分析---网络字节序与主机字节序
查看>>
linux sh/bash 编程常用
查看>>
x86寄存器和栈帧
查看>>
计算机科学经典论文(zz)
查看>>
ECC加密算法入门介绍
查看>>
文件系统与NoSQL分布式存储技术对比
查看>>