MockMvc单元测试

原始访问层代码

package com.example.demo.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: 
 * @Date: 2018/12/19 09:33
 * @Description:
 */
@RestController
public class test1Controller {

    @RequestMapping("/testDemo")
    public String test1(){
        System.out.println("这是第一个测试");
        return "这是一个测试测试";
    }
}

单元测试案例(1)

package com.example.demo.web;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;


/**
 * @Author: 
 * @Date: 2018/12/19 09:44
 * @Description:
 */

@SpringBootTest()
@RunWith(SpringRunner.class)

public class test1ControllerTest {

    private MockMvc mvc;

    @Before
    public void setUp() {
        mvc = MockMvcBuilders.standaloneSetup(new test1Controller()).build();
    }

    @Test
    public void test1() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/testDemo"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
    }

}

运行结果(1)

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2018-12-19 10:36:01.912  INFO 2668 --- [           main] c.example.demo.web.test1ControllerTest   : Starting test1ControllerTest on M7072XFZ with PID 2668 (started by maokai in F:\learnProject\demo)
2018-12-19 10:36:01.914  INFO 2668 --- [           main] c.example.demo.web.test1ControllerTest   : No active profile set, falling back to default profiles: default
2018-12-19 10:36:03.285  INFO 2668 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2018-12-19 10:36:03.575  INFO 2668 --- [           main] c.example.demo.web.test1ControllerTest   : Started test1ControllerTest in 2.076 seconds (JVM running for 2.871)
2018-12-19 10:36:03.844  INFO 2668 --- [           main] o.s.mock.web.MockServletContext          : Initializing Spring TestDispatcherServlet ''
2018-12-19 10:36:03.845  INFO 2668 --- [           main] o.s.t.web.servlet.TestDispatcherServlet  : Initializing Servlet ''
2018-12-19 10:36:03.845  INFO 2668 --- [           main] o.s.t.web.servlet.TestDispatcherServlet  : Completed initialization in 0 ms
这是第一个测试

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /testDemo
       Parameters = {}
          Headers = {}
             Body = <no character encoding set>
    Session Attrs = {}

Handler:
             Type =
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值