代码
package com.example.springboot;
import java.io.UnsupportedEncodingException;
import org.junit.Before;
import org.junit.jupiter.api.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.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.example.springboot.entity.Blog;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
@SpringBootTest(classes = {Application.class})
@Slf4j
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
class ApplicationTests {
@Autowired
MockMvc mockMvc;
@Test
public void testget() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("blogId", "2");
jsonObject.put("title", "IKUN");
jsonObject.put("body", "ikunyyds");
jsonObject.put("userId", "1");
MvcResult mvcResult = null;
try {
mvcResult = mockMvc.perform(
MockMvcRequestBuilders.put("/api/blog/update"). // 访问路径
contentType(MediaType.APPLICATION_JSON).
content(jsonObject.toString())). // 传入request参数
andReturn();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}