[Spring]~@SpringBootTest(单元测试)

[Spring]~@SpringBootTest(单元测试)1.添加Maven依赖<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><parent><groupId>org.springfram…

大家好,又见面了,我是你们的朋友全栈君。

1. 添加Maven依赖

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
 
 

    2. 编写启动入口类

    @SpringBootApplication
    public class StartUpApplication { 
       
        public static void main(String[] args) {
            SpringApplication.run(StartUpApplication.class, args);
        }
    }
     
     
     

      3. 编写Controller类

      @RestController
      public class HelloController { 
         
      
          @RequestMapping("/")
          public String index() {
              return "Hello Spring Boot,Index!";
          }
      
          @RequestMapping(value = "/test", method = RequestMethod.GET)
          public String test() {
              return "Spring Boot Test Demo!";
          }
      }
       
       
       

        4. 编写测试

        @RunWith(SpringRunner.class)
        @SpringBootTest(classes = StartUpApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
        public class HelloControllerTest { 
           
        
            /** * @LocalServerPort 提供了 @Value("${local.server.port}") 的代替 */
            @LocalServerPort
            private int port;
        
            private URL base;
        
            @Autowired
            private TestRestTemplate restTemplate;
        
            @Before
            public void setUp() throws Exception {
                String url = String.format("http://localhost:%d/", port);
                System.out.println(String.format("port is : [%d]", port));
                this.base = new URL(url);
            }
        
            /** * 向"/test"地址发送请求,并打印返回结果 * @throws Exception */
            @Test
            public void test1() throws Exception {
        
                ResponseEntity<String> response = this.restTemplate.getForEntity(
                        this.base.toString() + "/test", String.class, "");
                System.out.println(String.format("测试结果为:%s", response.getBody()));
            }
         
         
         

          其中,classes属性指定启动类,SpringBootTest.WebEnvironment.RANDOM_PORT经常和测试类中@LocalServerPort一起在注入属性时使用。会随机生成一个端口号。

          总结

          我们发现,随着Spring boot 版本的提升,单元测试变得更简单了。

          版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

          发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/139661.html原文链接:https://javaforall.net

          (0)
          全栈程序员-站长的头像全栈程序员-站长


          相关推荐

          • android之如何在两个activity之间传递handler_利用broadcast广播机制「建议收藏」

            这算是如何在两个activity之间传递handler的解决方案二了,解决方案一见http://blog.csdn.net/jason0539/article/details/18055259再重复一遍我遇到的问题,就是在MainActivity里面打开AnotherActivity去执行一些操作,相应的改变MainActivity里的一些布局或者执行一些动作,最开始想到的就是把MainAct

            2022年3月11日
            49
          • Django设置超时时间_Django orm

            Django设置超时时间_Django orm前言我们都知道时区,标准时区是UTC时区,django默认使用的就是UTC时区,所以我们存储在数据库中的时间是UTC的时间,但是当我们做的网站只面向国内用户,或者只是提供内部平台使用,我们希望存储在

            2022年7月30日
            8
          • win10系统搭建vagrant时开启bios,虚拟化问题

            win10系统搭建vagrant时开启bios,虚拟化问题

            2022年2月8日
            43
          • java笔试题库_java笔试题50道 收藏版

            java笔试题库_java笔试题50道 收藏版1、在JavaEE中,Servlet是在服务器端运行,以处理客户端请求而做出的响应的程序,下列选项中属于Servlet生命周期阶段的是()A、加载和实例化B、初始化C、服务D、销毁E、以上全部答案:E2、在JavaEE中的MVC设计模式中,()负责接受客户端的请求数据A、JavaBeanB、JSPC、ServletD、HTML答案:C3、过滤器应实现的接口是()。A、HttpServle…

            2022年7月7日
            29
          • BufferedWriter写int型数据

            BufferedWriter写int型数据在做项目的过程中遇到用BufferedWriter.writer(…)写文件的,但是在写入int型数据时是乱码。在翻阅了API后发现,BufferedWriter.writer(intc)方法写的不是一个int型数据,而是一个character型数据:因此,在用BufferedWriter.writer写数据的时候,如果要写int型数据,可以先把它转成String型的数据,这样就

            2022年6月10日
            44
          • 8分钟完成NodeJs爬虫,把JRS小姐姐全部看个遍

            本文讲的是利用nodejs以及相关库,爬取JRS爆照区内的爆照贴,并保存相关数据到本地。依赖选择constsuperagent=require(‘superagent’);//nodejs里一个非常方便的客户端请求代理模块constcheerio=require(‘cheerio’);//Node.js版的jQueryconstasync=r…

            2022年4月9日
            55

          发表回复

          您的邮箱地址不会被公开。 必填项已用 * 标注

          关注全栈程序员社区公众号