Springboot2 Tests

非 Y 不嫁゛ 提交于 2020-04-22 01:50:56
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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.request.MockMvcRequestBuilders;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SyncServerBootstrap.class) //类似前置条件,比如我需要启动服务之后才能请求
@Slf4j
@AutoConfigureMockMvc
public class QueryTests {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void greatUserWhithDetailView() throws Exception{
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","111");

        String result = mockMvc.perform(MockMvcRequestBuilders.post("/query")
                .contentType(MediaType.APPLICATION_JSON)
                .content(jsonObject.toJSONString()))
                .andReturn().getResponse().getContentAsString();
        log.info(result);
    }
}
@PostMapping(value = "/query", produces = "application/json;charset=utf-8")
public String queryPage(@RequestBody QueryVO query){
	//coding 
}
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
	
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
 </dependency>

Google Chrome Plugin :Talend API

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!