使用Spring Boot REST API进行测试驱动开发

栏目: 编程工具 · 发布时间: 5年前

内容简介:Maven插件rest-assured 是Java DSL测试REST服务,这个插件需要groovy-all来运行测试。我们将添加maven-failsafe-plugin插件来执行集成测试:测试类代码如下:

Maven插件rest-assured 是Java DSL测试REST服务,这个插件需要groovy-all来运行测试。

我们将添加maven-failsafe-plugin插件来执行集成测试:

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-failsafe-plugin</artifactId>
			<executions>
				<execution>
					<id>integration-test</id>
					<goals>
						<goal>integration-test</goal>
					</goals>
				</execution>
				<execution>
					<id>verify</id>
					<phase>verify</phase>
					<goals>
						<goal>verify</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<executions>
				<execution>
					<id>pre-integration-test</id>
					<goals>
						<goal>start</goal>
					</goals>
				</execution>
				<execution>
					<id>post-integration-test</id>
					<goals>
						<goal>stop</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

测试类代码如下:


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class)
@TestPropertySource(value={"classpath:application.properties"})
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class SpringRestControllerTest {
@Value(
"${server.port}")
int port;
@Test
public void getDataTest() {
get(
"/api/tdd/responseData").then().assertThat().body("data", equalTo("responseData"));
}
@Before
public void setBaseUri () {
RestAssured.port = port;
RestAssured.baseURI =
"http://localhost"; // replace as appropriate
}
}

@RunWith(SpringJUnit4ClassRunner.class)支持加载Spring应用程序上下文。

@ContextConfiguration 定义类级元数据,用于确定如何为集成测试加载和配置ApplicationContext。

@TestPropertySource 用于配置属性文件的位置。

@SpringBootTest 告诉Spring Boot去寻找一个主配置类(例如,一个带有@SpringBootApplication的类)并使用它来启动Spring应用程序上下文。

由于我们尚未定义REST端点,因此我们可以使用该mvn verify 命令运行上述测试 以查看测试失败。

让我们通过引入REST控制器来修复我们的测试:


@RestController
public class SpringRestController {
@RequestMapping(path = "/api/tdd/{data}", method= RequestMethod.GET)
public Response getData(@PathVariable(
"data") String data) {
return new Response(data);
}
//inner class
class Response {
private String data;
public Response(String data) {
this.data = data;
}
public String getData() {
return data;
}
}
}

在上面的类中,我们有一个REST端点,它接受“data”作为输入,并在响应体中返回一个新的“Response”实例。

让我们mvn verify 再次运行命令。它现在应该成功运行测试。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Photoshop Anthology

The Photoshop Anthology

Corrie Haffly / SitePoint Pty. Ltd. / 2006 / USD 39.95

The Photoshop Anthology is full-color, question-and-answer book for Web Designers who want to use Photoshop to build Websites and create better looking web graphics more effectively. The book covers: ......一起来看看 《The Photoshop Anthology》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码