首页 > 编程语言 > 详细

【spring-boot】写一个简单的单元测试

时间:2020-03-16 20:31:25      阅读:93      评论:0      收藏:0      [点我收藏+]

application.yml

server:
  port: 8090
  servlet:
    context-path: /springboot

HelloController

package com.komiles.study.controller;

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

@RestController
@RequestMapping("/hello")
public class HelloController {

    @GetMapping("/test")
    public String test(@RequestParam(value = "name", defaultValue = "1234")  String name)
    {
        return name;
    }
}

HelloControllerTest

package com.komiles.study.controller;

import org.junit.Test;
import org.junit.runner.RunWith;
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.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.MockMvcResultMatchers;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void test() throws Exception {
        this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/test"))
                .andExpect(MockMvcResultMatchers.status().isOk());
    }
}

 

【spring-boot】写一个简单的单元测试

原文:https://www.cnblogs.com/wangkongming/p/12505699.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!