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)
  • 相关阅读:
    Apache-Tomcat 和 Apache-Maven配置
    php版本CKFinder3.4.4自定义上传文件位置
    php版本CKEditor 4和CKFinder安装及配置
    windows10 卸载 Docker 和 DockerNAT
    faker php测试数据库生成2
    faker php测试数据库生成
    我的php.ini上传文件配置
    同一个网址电脑手机访问显示不同内容思路
    mysql where 条件中的字段有NULL值时的sql语句写法
    Chrome导出书签瘦身,去除ADD_DATE和ICON
  • 原文地址:https://www.cnblogs.com/architectforest/p/14447453.html
Copyright © 2011-2022 走看看