REST Service 开发 - 测试驱动开发
【摘要】本文介绍了使用 Spring Boot 完成 REST 服务集成测试的方法。为了方便访问 Web 服务,
这里使用了 Feign 作为访问与测试 REST 服务的工具,具有简单、易用性。
1. 概述
集成测试与开发同时进行,有助于加快系统开发,为团队提供高品质的代码。Spring boot
提供了基于 JUnit 的集成环境,让我们像测试普通函数一样测试 Web 服务。
2. 配置与代码
1. 添加 maven 配置
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-core</artifactId>
<version>8.18.0</version>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-jackson</artifactId>
<version>8.18.0</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- feign-core 是 Feign 处理 HTTP 模板库的核心库
- feign-jackson 是 json 格式 messagebody 对象读写库
- spring-boot-starter-test 是 spring boot test 的 bom 清单
2. 测试代码
在 test 目录下创建 resources 目录,并设为测试资源根
在 resources 目录,创建 application.properties 文件
server.port = 8080
更多配置见:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
在测试代码根目录(java),创建测试包,例如:
com.me.test.springboot
创建类 RestAppTest,代码如下:
package com.me.test.springboot;
import feign.*;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleRestApplication.class)
@WebIntegrationTest
public class RestAppTest {
public interface TestService {
@RequestLine("GET /customer/id/{id}")
String getOwner(@Param(value = "id") String name);
@RequestLine("GET /customer/addObj/")
@Headers({"Content-Type: application/json","Accept: application/json"})
Customer addCustomer(Customer customer);
}
String baseURL = "http://127.0.0.1:8080/services";
@Test
public void testGetCustomerName() throws Exception {
TestService service = Feign.builder()
.options(new Request.Options(1000, 3500))
.retryer(new Retryer.Default(5000, 5000, 3))
.target(TestService.class,baseURL);
assertEquals("hello123",service.getOwner("123"));
}
@Test
public void testAddCustom() throws Exception {
TestService service = Feign.builder()
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.options(new Request.Options(1000, 3500))
.retryer(new Retryer.Default(5000, 5000, 3))
.target(TestService.class,baseURL);
Customer customer = new Customer();
customer.setId(10);
customer.setName("Mary");
Customer ret = service.addCustomer(customer);
assertEquals(customer.getName(),ret.getName());
}
}
3. 代码要点
- 使用 JUnit 运行测试。 @RunWith(SpringJUnit4ClassRunner.class)
- Spring 上下文配置。 @SpringApplicationConfiguration(classes = SampleRestApplication.class)
- 启动 Servelet 服务。 @WebIntegrationTest
Feign 的模板就很简单了。例如:
@RequestLine("GET /customer/addObj/")
@Headers({"Content-Type: application/json","Accept: application/json"})
Customer addCustomer(Customer customer);
直接写 HTTPRequest 的三段文本中必要项,这对于 web 服务编程来说是最简单的。
成为云服务、Android 客户端访问云服务的主要库,也是有自己的特色的。
Feign.builder()
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
直接给出读写 JSON 的工具。
3. 运行
既可以用 maven 运行,也可以用在 IDEA 中运行。