加入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
测试时,需要在Test类上加两句注解
@RunWith(SpringRunner.class)
@SpringBootTest
基本测试,以测service为例:
package com.ah.test;
import org.junit.Assert;
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.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SbTest4Service {
@Autowired
UserService service;
@Test
public void m() {
System.out.println("---使用断言做一个测试");
Assert.assertTrue(service.isLogin());
}
}
@Service
class UserService {
public boolean isLogin() {
return true;
}
}
测Controller,比较麻烦,因为正常运行时,是通过浏览器访问Controller。
SpringBoot使用MockMvc进行控制层测试。
mock:模拟的。
以下示例,使用了Restfull风格API,分别演示了MockMvc测试的四种情况:
- 分段写法
- 链式写法
- 传json
- 获取json
package com.ah.test;
import javax.servlet.http.HttpSession;
import org.junit.Before;
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.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.*;
import org.springframework.test.web.servlet.result.*;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SbTest4Controller {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc moc;
private MockHttpSession session;
@Before
public void setupMockMvc() {
moc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
session = new MockHttpSession();
// 设置已登录用户(很多时候网站都会有登录要求)
session.setAttribute("user", "Cat");
}
@Test
public void get() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/testCtrl/get");
builder = builder.session(session);
ResultActions resultActions = moc.perform(builder);
// 判断状态
ResultMatcher resultMatcher = MockMvcResultMatchers.status().isOk();
resultActions = resultActions.andExpect(resultMatcher);
}
@Test
public void insert() throws Exception {
// 链式写法
moc.perform(MockMvcRequestBuilders.post("/testCtrl/insert").session(session))
.andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void update() throws Exception {
// 传json(控制器需要@RequestBody)
String json = "{"id":"123"}";
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.put("/testCtrl/update")
.contentType(MediaType.APPLICATION_JSON_UTF8).accept(MediaType.APPLICATION_JSON_UTF8)
.content(json.getBytes()).session(session);
moc.perform(builder).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void delete() throws Exception {
// 判断返回Json是否正确,并查看返回信息(可以看全部返回的json)
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.delete("/testCtrl/delete").session(session);
moc.perform(builder).andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value("456")).andDo(MockMvcResultHandlers.print());
;
}
}
@RestController
@RequestMapping("/testCtrl")
class MyController {
@GetMapping(value = "/get")
public void get(HttpSession session) {
System.out.println("get:" + session.getAttribute("user"));
}
@PostMapping(value = "/insert")
public void insert() {
System.out.println("---insert");
}
@PutMapping(value = "/update")
public void update(@RequestBody String json) {
System.out.println("---json = " + json);
System.out.println("---update");
}
@DeleteMapping(value = "/delete")
public String delete() {
System.out.println("---delete");
return "{"id":"456"}";
}
}