栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot单元测试

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot单元测试

pow.xml
 
      org.springframework.boot
      spring-boot-starter-test
     
      org.springframework.boot
      spring-boot-starter-test
    
Service业务
package com.xxx.springboot.service;

import com.xxx.springboot.Starter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;


@RunWith(SpringRunner.class)
//此处classes内的内容是@SpringBootApplication入口
@SpringBootTest(classes = {Starter.class})
public class UserServiceTest {
    private Logger log= LoggerFactory.getLogger(UserServiceTest.class);
    @Resource
    private UserService userService;
    @Before
    public void before() {
        System.out.println("单元测试开始");
    }
    @Test
    public void test() {
        log.info(userService.test(1).toString());
    }
    @After
    public void after() {
        System.out.println("单元测试结束");
    }
}

Controller业务
package com.xxx.springboot.controller;

import com.xxx.springboot.Starter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.mock.web.MockHttpServletResponse;
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.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Starter.class})
@AutoConfigureMockMvc
public class UserControllerTest {
    private Logger log= LoggerFactory.getLogger(UserControllerTest.class);
    @Autowired
    private MockMvc mockMvc;
    //用户查询
    @Test
    public void apiTest() throws Exception {
        //构建请求
        MockHttpServletRequestBuilder request= MockMvcRequestBuilders.get("/user/1")
                .contentType("text/html") //指定请求的contentType头信息
                .accept(MediaType.APPLICATION_JSON); //指定请求的Accept头信息
        //发送请求,获取结果
        ResultActions perform=mockMvc.perform(request);
        //请求结果检验
        perform.andExpect(MockMvcResultMatchers.status().isOk());
        //表示执行后响应结果
        MvcResult mvcResult=perform.andReturn();
        MockHttpServletResponse response=mvcResult.getResponse();
        log.info("响应状态: {}",response.getStatus());
        log.info("响应内容{}",response.getContentAsString());
    }
}

可简写为

    public void apiTest() throws Exception {
//        //构建请求
//        MockHttpServletRequestBuilder request= MockMvcRequestBuilders.get("/user/1")
//                .contentType("text/html") //指定请求的contentType头信息
//                .accept(MediaType.APPLICATION_JSON); //指定请求的Accept头信息
//        //发送请求,获取结果
//        ResultActions perform=mockMvc.perform(request);
//        //请求结果检验
//        perform.andExpect(MockMvcResultMatchers.status().isOk());
//        //表示执行后响应结果
//        MvcResult mvcResult=perform.andReturn();
//        MockHttpServletResponse response=mvcResult.getResponse();
        MvcResult mvcResult=mockMvc.perform(MockMvcRequestBuilders.get("/user/1")).
                andExpect(MockMvcResultMatchers.status().isOk()).andReturn();

        log.info("响应状态: {}",mvcResult.getResponse().getStatus());
        log.info("响应内容{}",mvcResult.getResponse().getContentAsString());
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/720134.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号