2019-06-14 20:49:59
浏览数 (1)
添加了 spring-boot-starter-test
依赖
BaseControllerTest
代码语言:javascript
复制package com.test;
import org.hamcrest.Matcher;
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.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultHandler;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.*;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional(transactionManager = "transactionManager", rollbackFor = Exception.class)
public class BaseControllerTest{
@Autowired
private WebApplicationContext wac;
protected MockMvc mockMvc;
protected HttpHeaders headers;
@Before
public void setUp() {
headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
headers.add("timestamp", "1555405660290.835");
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void contextLoads() {
}
protected MockHttpServletRequestBuilder post(String url) {
return MockMvcRequestBuilders.post(url).headers(this.headers);
}
protected MockHttpServletRequestBuilder get(String url) {
return MockMvcRequestBuilders.get(url).headers(this.headers);
}
protected MockHttpServletRequestBuilder delete(String url) {
return MockMvcRequestBuilders.delete(url).headers(this.headers);
}
protected MockHttpServletRequestBuilder put(String url) {
return MockMvcRequestBuilders.put(url).headers(this.headers);
}
protected MockHttpServletRequestBuilder mutipartOfFile(String url, String filePath) throws IOException {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
MockMultipartFile mockMultipartFile = new MockMultipartFile("file", file.getName(), "*/*", fis);
return MockMvcRequestBuilders
.multipart(url)
.file(mockMultipartFile)
.contentType(MediaType.MULTIPART_FORM_DATA)
.headers(this.headers);
}
protected ViewResultMatchers view() {
return MockMvcResultMatchers.view();
}
protected ModelResultMatchers model() {
return MockMvcResultMatchers.model();
}
protected ResultHandler print() {
return MockMvcResultHandlers.print();
}
protected StatusResultMatchers status() {
return MockMvcResultMatchers.status();
}
protected ContentResultMatchers content() {
return MockMvcResultMatchers.content();
}
protected CookieResultMatchers cookie() {
return MockMvcResultMatchers.cookie();
}
protected JsonPathResultMatchers jsonPath(String expression, Object... args) {
return MockMvcResultMatchers.jsonPath(expression, args);
}
protected <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher) {
return MockMvcResultMatchers.jsonPath(expression, matcher);
}
}
具体业务测试类
代码语言:javascript
复制package com.test;
import org.junit.Test;
/**
* @author qubianzhong
* @date 2019/5/15 21:02
*/
public class FortuneControllerTest extends BaseControllerTest{
@Test
public void starFortuneTest() throws Exception {
// uri
String uri = "/fortune/star";
mockMvc.perform(get(uri))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0));
}
}