zoukankan      html  css  js  c++  java
  • springBoot单元测试-基础单元测试

    1)在pom文件中加入junit支持

    1     <!-- spring-boot-starter-test 单元测试 -->
    2     <dependency>
    3         <groupId>org.springframework.boot</groupId>
    4         <artifactId>spring-boot-starter-test</artifactId>
    5         <scope>test</scope>
    6     </dependency>

    2)新建测试类 Application1Test

    在class 头上增加注解

    @RunWith(SpringRunner.class)  :指定运行平台,点进去 SpringRunner.class 可以看到他是集成自 SpringJUnit4ClassRunner 
    @SpringBootTest : 标记此类为方法测试类,在加载和其他扫描是spring不会去对他进行扫描

     1 @RunWith(SpringRunner.class) 
     2 @SpringBootTest
     3 public class Application1Test {
     4 
     5     
     6     @Test
     7     public void test1() {
     8         
     9         //业务逻辑代码
    10         System.err.println("this is  test1 !");
    11         
    12     }
    13     @Test
    14     public void test2() {
    15         
    16         //业务逻辑代码
    17         System.err.println("this is  test2 !");
    18         
    19     }
    20     
    21     // @Before 标记 在执行Test 前执行该方法
    22     @Before
    23     public void testBefore() {
    24         
    25         //业务逻辑代码
    26         System.err.println("this is  testBefore !");
    27         
    28     }
    29     
    30     // @After 标记 在执行Test 后执行该方法
    31     @After
    32     public void testAfter() {
    33         
    34         //业务逻辑代码
    35         System.err.println("this is  testAfter !");
    36         
    37     }
    38     
    39 
    40 
    41 }

    认为 springBoot的实际运用场景并不是很多,但可以用在像计算一段复杂代码的执行时间,比如数据库连接耗时等需求上

  • 相关阅读:
    .Net需要掌握的知识
    图片轮播
    vector
    2016ACM青岛区域赛题解
    总是有一个程序的bug没找到
    poj1001_Exponentiation_java高精度
    poj2236_并查集_Wireless Network
    poj1703_Find them, Catch them_并查集
    poj2492_A Bug's Life_并查集
    poj1182食物链_并查集_挑战程序设计竞赛例题
  • 原文地址:https://www.cnblogs.com/cx987514451/p/9304019.html
Copyright © 2011-2022 走看看