使用spring-boot-test做测试
程序员文章站
2022-03-07 22:37:55
...
一,控制层测试
package demo.springdatajap.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping(name="/",method=RequestMethod.GET)
public String get(String name) {
return "hello,"+name;
}
@RequestMapping(name="/",method=RequestMethod.POST)
public String post(String name) {
return "hello,"+name;
}
}
package demo.springdatajap.test;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import demo.springdatajap.SpringbootApplication;
@RunWith(SpringRunner.class)
//classes = SpringbootApplication.class SpringbootApplication为启动类
@SpringBootTest(classes = SpringbootApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserControllerTest {
/**
* 端口号
*/
@LocalServerPort
private int port;
/**
* 基础路径
*/
private URL base;
@Autowired
private TestRestTemplate restTemplate;
@Before
public void init() throws Exception {
String url = String.format("http://localhost:%d/", port);
this.base = new URL(url);
}
/**
* get请求测试
* @throws Exception
*/
@Test
public void get() throws Exception {
//请求路径
String url = this.base.toString()+"user?name={name}";
//请求参数
Map<String, String> urlVariables = new HashMap<>();
urlVariables.put("name", "get");
ResponseEntity<String> response = this.restTemplate.getForEntity(url, String.class,urlVariables);
System.out.println("测试结果为:" + response.getBody());
}
/**
* post请求测试
* @throws Exception
*/
@Test
public void post() throws Exception {
//请求路径
String url = this.base.toString()+"user";
//请求参数
MultiValueMap<String, String> urlVariables = new LinkedMultiValueMap<String, String>();
urlVariables.add("name", "post");
ResponseEntity<String> response = this.restTemplate.postForEntity(url, urlVariables, String.class);
System.out.println("测试结果为:" + response.getBody());
}
}
二,业务层测试
package demo.springdatajap.test;
import java.util.List;
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.test.context.junit4.SpringJUnit4ClassRunner;
import demo.springdatajap.SpringbootApplication;
import demo.springdatajap.entity.User;
import demo.springdatajap.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
//classes = SpringbootApplication.class SpringbootApplication为启动类
@SpringBootTest(classes = SpringbootApplication.class)
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void test() {
try {
List<User> list = userService.findUserList();
for (User user : list) {
System.out.println(user.getName());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
三,资料参考
使用@SpringBootTest注解进行单元测试:https://blog.csdn.net/limenghua9112/article/details/79694849
Spring Boot 单元测试详解+实战教程:https://www.cnblogs.com/javastack/p/9150408.html
springboot测试service层的单元测试:https://www.cnblogs.com/xubiao/p/7568257.html
上一篇: dokcer入门
下一篇: springboot test
推荐阅读
-
memcache使用方法测试 # 转自 简单--生活 #,
-
小米9 Pro 5G 7小时重度使用测试成绩公布:大幅超越小米9
-
surging如何使用swagger 组件测试业务模块
-
使用XHGui来测试PHP性能的教程
-
php中mysql连接和基本操作代码(快速测试使用,简单方便)_PHP
-
使用 Postman 测试 API
-
使用junit进行单元测试时报错Invalid bound statement (not found)
-
使用Nginx反向代理最前端,多个Docker容器做后端。将多台服务器整合到一台服务器上
-
mysql使用status命令测试/判断数据库是否连接
-
测试框架nunit之assertion断言使用详解