1import com.alibaba.fastjson.JSONObject; 2import lombok.extern.slf4j.Slf4j; 3import org.junit.Test; 4import org.junit.runner.RunWith; 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 7import org.springframework.boot.test.context.SpringBootTest; 8import org.springframework.http.MediaType; 9import org.springframework.test.context.junit4.SpringRunner; 10import org.springframework.test.web.servlet.MockMvc; 11import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 12 13@RunWith(SpringRunner.class) 14@SpringBootTest(classes = SyncServerBootstrap.class) //类似前置条件,比如我需要启动服务之后才能请求 15@Slf4j 16@AutoConfigureMockMvc 17public class QueryTests { 18 19 @Autowired 20 private MockMvc mockMvc; 21 22 @Test 23 public void greatUserWhithDetailView() throws Exception{ 24 JSONObject jsonObject = new JSONObject(); 25 jsonObject.put("name","111"); 26 27 String result = mockMvc.perform(MockMvcRequestBuilders.post("/query") 28 .contentType(MediaType.APPLICATION_JSON) 29 .content(jsonObject.toJSONString())) 30 .andReturn().getResponse().getContentAsString(); 31 log.info(result); 32 } 33} 34 35 36@PostMapping(value = "/query", produces = "application/json;charset=utf-8") 37public String queryPage(@RequestBody QueryVO query){ 38 //coding 39} 40 41 42 <parent> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-starter-parent</artifactId> 45 <version>2.2.2.RELEASE</version> 46 <relativePath/> <!-- lookup parent from repository --> 47 </parent> 48 49<dependency> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-starter-test</artifactId> 52 <scope>test</scope> 53 </dependency>
Google Chrome Plugin :Talend API 