SpringBoot测试类的编写

1,测试类示例:

package com.gwm.ma.sms.api.domain.test.controller;
import cn.hutool.json.JSONUtil;
import com.gwm.ma.sms.api.domain.sendTask.controller.form.SmsSendParamForm;
import com.gwm.ma.sms.api.domain.signature.controller.form.*;
import com.gwm.ma.sms.api.domain.template.controller.form.*;
import com.gwm.ma.sms.api.domain.test.controller.form.TestForm;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
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.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.springframework.test.web.servlet.result.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();
        testForm.setName("zhangsan");
        String content = JSONUtil.toJsonStr(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>

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,我们可以使用Spring Test和Spring Boot Test来编写单元测试。Spring Test提供了对Spring应用程序的单元测试支持,而Spring Boot Test则提供了对Spring Boot应用程序的单元测试支持。通过使用这些测试框架,我们可以方便地编写和运行各种型的单元测试。 对于测试HTTP客户端,可以使用@RestClientTest注解来标记测试。该注解会自动配置Spring Boot上下文,并为测试中的HTTP客户端提供支持。这样我们就可以方便地测试与外部API的交互。 如果需要测试整个Spring Boot应用程序的集成,可以使用@SpringBootTest注解来标记测试。该注解会自动配置Spring Boot上下文,并加载整个应用程序的配置。这样我们就可以对整个应用程序进行集成测试,包括测试数据库、缓存和其他外部依赖。 关于如何编写Spring Boot单元测试的更多详细信息,可以参考中提供的链接,其中有一篇关于Spring Test的单元测试的博文,可以帮助你更好地理解和使用Spring Boot中的单元测试功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Spring Boot中编写单元测试](https://blog.csdn.net/q15102780705/article/details/102562735)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [spring boot单元测试](https://blog.csdn.net/qq_35789269/article/details/119428548)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值