zoukankan      html  css  js  c++  java
  • 第一个SpringBoot测试实例

    1.SpringBoot项目构建:http://start-spring.io   自动化构建SpringBoot项目,保存在本地并解压

    2.安装gradle并配置gradle环境

    3.配置阿里云maven仓库:修改gradle.build 里面额maven仓库,改为:maven{  url 'http://maven.aliyun.com/nexus/content/groups/public'  }

    4.构建项目,进入解压后的项目路径下,打开控制台执行以下命令:cmd -> cd E:/hello ->E:  ->  gradle build  ->java -jar build/libs/xxxxx.jar

    5.导入开发工具,进行测试,测试代码如下:

    在main目录下,新建HelloController类:

    package com.waylau.spring.boot.blog.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
        @RequestMapping("/hello")
        public String hello(){
            return "Hello World!";
        }
    }

    在test下,新建HelloControllerTest类:

    package com.waylau.spring.boot.blog.contrller;
    
    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.http.MediaType;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    
    import static org.hamcrest.Matchers.equalTo;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    public class HelloControllerTest {
        @Autowired
        private MockMvc mockMvc;
        @Test
        public void testHello() throws Exception {
            mockMvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
                    .andExpect(status().isOk())
                    .andExpect(content().string(equalTo("Hello World!")));
        }
    
    }
  • 相关阅读:
    17. Letter Combinations of a Phone Number
    16. 3Sum Closest
    15. 3Sum
    14. Longest Common Prefix
    13. Roman to Integer
    12. Integer to Roman
    11. Container With Most Water
    10. Regular Expression Matching
    9. Palindrome Number
    8. String to Integer (atoi)
  • 原文地址:https://www.cnblogs.com/zk-blog/p/10344082.html
Copyright © 2011-2022 走看看