Spring Boot Test 学习
2018-03-13 11:28
344 查看
1. org.springframework.test.context.junit4.SpringRunner
SpringRunner is an alias for the SpringJUnit4ClassRunner.To use this class, simply annotate a JUnit 4 based test class with {@code @RunWith(SpringRunner.class)}.
public final class SpringRunner extends SpringJUnit4ClassRunner {...}2. 创建Spring Boot测试类@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest{
@Autowired
UserRepoistory repository;
@Test
public void testSomeCall(){
//...
}
}Tips: @SpringBootTest将会调用采用@SpringBootApplication声明的类去创建ApplicationContext.
3. 测试rest接口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.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class GreetingControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void hello() {
String body = this.restTemplate.getForObject("/hello", String.class);
assertThat(body).isEqualTo("Hello world!");
}
}Tips: 测试当前项目的rest接口时,不需要加项目名。比如当前项目名为greeting, rest路径为/hello, 直接访问
/hello就可以,不需要额外添加项目名/greeting.
SpringRunner is an alias for the SpringJUnit4ClassRunner.To use this class, simply annotate a JUnit 4 based test class with {@code @RunWith(SpringRunner.class)}.
public final class SpringRunner extends SpringJUnit4ClassRunner {...}2. 创建Spring Boot测试类@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest{
@Autowired
UserRepoistory repository;
@Test
public void testSomeCall(){
//...
}
}Tips: @SpringBootTest将会调用采用@SpringBootApplication声明的类去创建ApplicationContext.
3. 测试rest接口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.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class GreetingControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void hello() {
String body = this.restTemplate.getForObject("/hello", String.class);
assertThat(body).isEqualTo("Hello world!");
}
}Tips: 测试当前项目的rest接口时,不需要加项目名。比如当前项目名为greeting, rest路径为/hello, 直接访问
/hello就可以,不需要额外添加项目名/greeting.
相关文章推荐
- Spring boot 学习笔记(04)—— 多环境配置(dev,test,pro)
- SpringBoot学习教程 - 01 - 第一个SpringBoot项目
- Spring Boot学习之旅:(十五)使用JdbcTemplate
- Spring Boot学习笔记-错误处理及自定义
- SpringBoot学习-(十一)SpringBoot过滤器
- SpringBoot学习(六)使用Thymeleaf布局
- springboot学习(二) Spring Boot 快速入门
- 基于Spring Boot和Spring Cloud实现微服务架构学习(一)-Spring框架介绍
- Spring Boot 学习
- spring boot test
- SpringBoot学习历程(4)->全局异常的处理
- SpringBoot学习——websocket组件学习在线聊天室demo
- springboot学习总结(一)
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- Spring boot 和 mybatis 学习笔记3--动态sql
- 一步一步学习:1、Spring-boot初步认识
- Spring Boot学习笔记----POI(Excel导入导出)
- springboot Test 自动配置注解详单
- spring boot 深入学习(继承结构)