zoukankan      html  css  js  c++  java
  • Spring Boot-2 (@GetMapping注解)

    一、 @GetMapping注解 简单实现以及如何测试

    1.首先创建一个类 标明注解@SpringBootApplication

    并且在main函数中写SpringApplication.run(类名.class, args)

    2.建立一个类写controller,标明注解@RestController

    在类中写一个函数实现get请求

    标明注解@GetMapping

    写法:

    @GetMapping("/api/users")  --------> 不加参数写法

    如下图 

    3.对应测试的写法

    首先测试类上标明注解

    @SpringBootTest

    @AutoConfigureMockMvc

     

    其次,类中标明

    @Autowired

    private MockMvc mockMvc

    接着,测试的类方法上标明

    @Test

    方法中写要测试的内容 

     前两个@Test为对验证码200和返回内容的测试

       第三个@Test为上面两种写法的合并更方便,可以一次测完

        @Test
        void should_assert_status_responseBody() throws Exception {
            //发送的请求为get请求 URL为"/api/users"
            mockMvc.perform(get("/api/users"))
                    .andExpect(status().is(200))              // 期待返回的状态码为200
                    .andExpect(content().string("Obama"));    //期待返回的内容为Obama
        }
  • 相关阅读:
    thinkphp 视图定义
    ThinkPHP支持模型的分层
    thinkphp 虚拟模型
    thinkphp 参数绑定
    thinkphp 自动完成
    thinkphp 自动验证
    thinkphp 子查询
    thinkphp 动态查询
    ThinkPHP sql查询
    thinkphp 统计查询
  • 原文地址:https://www.cnblogs.com/manru75/p/11256315.html
Copyright © 2011-2022 走看看