本节演示一下在springboot中完成单元测试。
1、环境约束
- win10 64位操作系统
- idea2018.1.5
- maven-3.0.5
- jdk-8u162-windows-x64
2、前提约束
- 创建一个springboot项目,假设项目名称为bootunittest,主启动类DemoApplication.java
https://www.jianshu.com/p/de979f53ad80 - 在bootunittest/src/main/java中创建一个类UserController.java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class UserController {
@Resource
UserService userService;
@GetMapping("/user")
public String get(@RequestBody User user)
{
return userService.get(user);
}
@PostMapping("/user")
public String post(@RequestBody User user)
{
return userService.post(user);
}
}
- 在bootunittest/src/main/java中创建一个类UserService.java
import org.springframework.stereotype.Service;
@Service
public class UserService {
public String get(User user) {
return "get";
}
public String post(User user) {
return "post";
}
}
- 在bootunittest/src/main/java中创建一个类User.java
import java.io.Serializable;
public class User implements Serializable {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public User() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
3、操作步骤
- 在bootunittest/src/test/java中创建一个测试类UnitTest.java
import com.alibaba.fastjson.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
@AutoConfigureMockMvc
public class DemoApplicationTests {
@Resource
private MockMvc mockMvc;
@Resource
UserService userService;
@Test
//测试service层,controller、mapper层一样
public void testService() {
String ret = userService.get(new User(1,"ali"));
System.out.println(ret);
Assert.assertEquals("get",ret);
}
@Test
//测试post api
public void postTest() throws Exception {
User user = new User(1,"ali");
MvcResult mvcResult = mockMvc.perform( MockMvcRequestBuilders.post("/user").characterEncoding("UTF-8")
.contentType(MediaType.APPLICATION_JSON)
.content(JSONObject.toJSONString(user).getBytes())).andReturn();
int status = mvcResult.getResponse().getStatus();
System.out.println(status);
}
@Test
//测试get api
public void getTest() throws Exception {
User user = new User(1,"ali");
MvcResult mvcResult = mockMvc.perform( MockMvcRequestBuilders.get("/user").characterEncoding("UTF-8")
.contentType(MediaType.APPLICATION_JSON)
.content(JSONObject.toJSONString(user).getBytes())).andReturn();
int status = mvcResult.getResponse().getStatus();
System.out.println(status);
}
}
以上就是在springboot中完成单元测试的过程。