SpringBoot测试类的编写1,测试类⽰例:
package com.gwm.ma.sms.st.controller;
import cn.hutool.json.JSONUtil;
import com.gwm.ma.sms.api.ller.form.SmsSendParamForm;
import com.gwm.ma.sms.api.ller.form.*;
import com.gwm.ma.sms.ller.form.*;
import com.gwm.ma.sms.st.controller.form.TestForm;
slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.st.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.st.context.SpringBootTest;
import org.springframework.http.MediaType;
import st.web.servlet.MockMvc;
import st.web.servlet.RequestBuilder;
import st.quest.MockMvcRequestBuilders;
import static st.sult.MockMvcResultMatchers.status;
@Slf4j
@AutoConfigureMockMvc
@SpringBootTest
public class TestControllerTest {
@Autowired
TestController testController;
@Autowired
private MockMvc mockMvc;
@Test
public void test1() {
}
@Test
public void testPost() throws Exception {
// MockMvc mockMvc = new MockMvc();
TestForm testForm = new TestForm();适合新手的spring boot
testForm.setName("zhangsan");
String content = JsonStr(testForm);
//构造请求参数
RequestBuilder rb = MockMvcRequestBuilders.post("/test/post")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(content);
//发送请求,验证返回结果
String result = mockMvc.perform(rb)
.andExpect(status().isOk())
// .andExpect(MockMvcResultMatchers.jsonPath("$.data.name").value("zhangsan"))
.andReturn().getResponse().getContentAsString();
log.info(result);
}
2,所需要jar包
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId> </exclusion>
</exclusions>
</dependency>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论