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.Driver
url: jdbc:mysql://127.0.0.1:3306/jack
username: root
password: root
jpa:
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\u65BD
ddl-auto: update
#show-sql\u7528\u6765\u8BBE\u7F6Ehibernate\u64CD\u4F5C\u7684\u65F6\u5019\u5728\u63A7\u5236\u53F0\u663E\u793A\u5176\u771F\u5B9E\u7684sql\u8BED\u53E5
show-sql: true
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
jackson:
serialization:
#\u8BA9\u63A7\u5236\u5668\u8F93\u51FA\u7684json\u5B57\u7B26\u4E32\u683C\u5F0F\u66F4\u7F8E\u89C2
indent_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 {
@Autowired
private 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 {
@Autowired
private PersonRepository personRepository;
private MockMvc mockMvc;
@Autowired
WebApplicationContext webApplicationContext;
String expectedJson;
/*@Test
public void contextLoads() {
}*/
@Before
public 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);
}
@Test
public 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