• 单元测试spring-boot-starter-test


    参考博客: https://www.cnblogs.com/mzc1997/p/14306538.html

    配置pom

    junit-vintage-engine junit4
    junit-jupiter-engine junit5
    排除junit4使用junit5,两者在切换时要特别注意

    
    	org.springframework.boot
    	spring-boot-starter-test
    	test
    	
            
                org.junit.vintage
                junit-vintage-engine
            
        
    
    
    	org.junit.jupiter
    	junit-jupiter-engine
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    命名规范

    单元测试类的命名规范为:被测试类的类名+Test。.
    单元测试类中测试方法的命名规范为:test+被测试方法的方法名+AAA,其中AAA为对同一个方法的不同的单元测试用例的自定义名称。

    junit5的使用

    import org.junit.jupiter.api.AfterAll;
    import org.junit.jupiter.api.BeforeAll;
    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.ActiveProfiles;
    
    @ActiveProfiles("test")
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    @DisplayName("Base Test junit5才有") // 显示名称通常用于IDE和构建中的测试报告
    class DemoFlowfestApplicationTests {
    	@BeforeAll
    	public static void beforall(){
    		System.out.println("beforall***");
    	}
    
    	@Test
    	void contextLoads() {
    		System.out.println("run___1");
    	}
    	
        @Test
        void contextLoads2() {
            System.out.println("run___2");
        }
    
    	@AfterAll
    	public static void afterall(){
    		System.out.println("after****");
    	}
    
    }
    beforall***
    run___2
    run___1
    after****
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    @ActiveProfiles(“test”)
    需要配置resources
    在这里插入图片描述

    junit4

    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    // 让 JUnit 运行 Spring 的测试环境, 获得 Spring 环境的上下文的支持
    @RunWith(SpringRunner.class)
    // 获取启动类,加载配置,确定装载 Spring 程序的装载方法,它回去寻找 主配置启动类(被 @SpringBootApplication 注解的)
    @SpringBootTest(value={"com.example.demoflowfest.DemoFlowfestApplication"}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    //这里一定要有public
    public class DemoFlowfestApplicationTestsTwo {
    	@Before
    	public void befer(){
    		System.out.println("befer***");
    	}
    
    	@Test
    	public void contextLoads() {
    		System.out.println("run___aa");
    	}
    
    	@Test
    	public void contextLoadsa() {
    		System.out.println("run___bb");
    	}
    
    	@After
    	public void after(){
    		System.out.println("after**");
    	}
    }
    返回值
    befer***
    run___bb
    after**
    befer***
    run___aa
    after**
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
  • 相关阅读:
    杠铃策略,给你的“B计划”一个“阶层跃升”的机会
    前端判断版本号区分接口请求的baseURL
    工厂设计模式
    ReentrantLock工作原理
    第七章节 Qt的UI界面设计详解
    差分+差分矩阵(更适合新手宝宝体质)
    【Rust】快速教程——从hola,mundo到所有权
    UE5.1_常用快捷键
    中国石油大学《计算机应用基础》第三次在线作业
    云尘 命令执行系列
  • 原文地址:https://blog.csdn.net/jinying_51eqhappy/article/details/133064419