本文共 7583 字,大约阅读时间需要 25 分钟。
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
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; }}
package com.pyc.springtest.dao;import com.pyc.springtest.domain.Person;import org.springframework.data.jpa.repository.JpaRepository;public interface PersonRepository extends JpaRepository{ }
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 ListfindAll(){ return personRepository.findAll(); }}
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 提供的,可用于在测试开始前进行一些初始化的工作。mvn clean package -Dmaven.test.skip=true
命令回车,等待程序的自动执行,再无错误的情况下,Terminal 窗口的结果界面如下: 转载地址:http://jlqgn.baihongyu.com/