Springboot2 Tests

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

评论

暂无评论