SpringBoot单元测试

pow.xml

 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency> 

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());
    }

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值