zoukankan      html  css  js  c++  java
  • spring boot:为service类做参数化单元测试(spring boot 2.4.3)

    一,演示项目的相关信息

    1,地址:

         https://github.com/liuhongdi/servicetest

    2,功能说明:演示给一个service生成测试文件

    3,项目结构:如图:

    说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

             对应的源码可以访问这里获取: https://github.com/liuhongdi/

    说明:作者:刘宏缔 邮箱: 371125307@qq.com

    二,如何创建一个测试文件?

    1,可以手动创建一个class文件

    2,在intellij idea中可以快速创建一个test文件:

       打开一个service类,在类名上执行:  ctrl+shift+T

       然后从弹出的窗口中选择:

        Create New Test

       如图:

     

    三,java代码说明

    1,service/HelloService.java

    package com.servicetest.demo.service;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class HelloService {
    
        public String sayHello(String name) {
            return "您好,"+name+"!";
        }
    
        public int addTwo(int first,int second) {
            return first+second;
        }
    }

    2,service/HelloServiceTest.java

    package com.servicetest.demo.service;
    
    import org.junit.jupiter.api.*;
    import org.junit.jupiter.params.ParameterizedTest;
    import org.junit.jupiter.params.provider.CsvSource;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import javax.annotation.Resource;
    
    import static org.hamcrest.MatcherAssert.assertThat;
    import static org.hamcrest.Matchers.*;
    import static org.hamcrest.Matchers.equalTo;
    import static org.junit.jupiter.api.Assertions.*;
    
    @SpringBootTest
    @DisplayName("测试HelloService")
    class HelloServiceTest {
        @Resource
        HelloService helloService;
    
        @BeforeAll
        static void initAll() {
            System.out.println("this is BeforeAll");
        }
    
        @AfterAll
        static void endAll() {
            System.out.println("this is AfterAll");
        }
    
    
        @BeforeEach
        public void initOne() {
            System.out.println("this is BeforeEach");
        }
    
        @AfterEach
        public void endOne() {
            System.out.println("this is AfterEach");
        }
    
        @Test
        @DisplayName("测试sayhello方法")
        void sayHello() {
            String hellostr = helloService.sayHello("老刘");
            assertThat(hellostr, equalTo("您好,老刘!"));
            assertThat(hellostr, hasLength(6));
            assertThat(hellostr, containsString("您好,"));
        }
    
        @ParameterizedTest
        @CsvSource({"1,2,3", "11,12,23", "33,100,133"})
        @DisplayName("参数化测试")
        void addTwo(int first,int second,int sum) {
            int rest = helloService.addTwo(first,second);
            assertThat(rest, equalTo(sum));
        }
    
    }

    四,测试效果

    五,查看spring boot的版本:

      .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )\___ | '_ | '_| | '_ / _` |    
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::                (v2.4.3)
  • 相关阅读:
    一个简单的Python字符串处理文件
    hexo + Github 搭建问题综述
    node scripts/install.js 停顿解决办法
    Mac下安装hexo Error: Cannot find module './build/Release/DTraceProviderBindings 解决
    Python学习札记(三十八) 面向对象编程 Object Oriented Program 9
    Python学习札记(三十七) 面向对象编程 Object Oriented Program 8 @property
    比较两个目录的文件是否相同
    python Polygon模块安装
    ls操作总结
    导入其他python文件或者python文件的函数
  • 原文地址:https://www.cnblogs.com/architectforest/p/14447453.html
Copyright © 2011-2022 走看看