package com.picchealth.hmims.si;
import com.fasterxml.jackson.databind.ObjectMapper;
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.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.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* @author Aikes
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {AikesServerApplication.class})//启动整个springboot工程
@AutoConfigureMockMvc
@WebAppConfiguration
public class DemoAikesTest {
private MockMvc mvc;
@Autowired
private WebApplicationContext context;
@Before
public void setupMockMvc() throws Exception {
mvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void testAikes() throws Exception {
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"name\":\"aikes\",\"age\":\"14\"}";
this.mvc.perform(
post("/aikes/testInterface")
.contentType(MediaType.APPLICATION_JSON_UTF8)
/* 使用writeValueAsString() 方法来获取对象的JSON字符串表示 */
.content(jsonString))
.andDo(print())
.andExpect(status().isOk());
}
}
SpringBoot项目Junit单元测试Demo类
最新推荐文章于 2024-03-20 07:43:50 发布