• Java单元测试


    工作很有用,记下来先

     1 package com.xujingyang.junit;
     2 
     3 import junit.framework.Assert;
     4 
     5 import org.junit.After;
     6 import org.junit.AfterClass;
     7 import org.junit.Before;
     8 import org.junit.BeforeClass;
     9 import org.junit.Test;
    10 
    11 public class junit {
    12     
    13     class test {
    14         public  int add(int x,int y){
    15             return x+y;
    16         }
    17         public  int odd(int x,int y){
    18             return x-y;
    19         }
    20     }
    21     //=============注意:@Test和@Before以及@After下的测试方法不能带static,而@BeforeClass和@AfterClass必须要带static;
    22     // ===========都不能有参数且无返回值=========================================
    23     
    24     test t;
    25     
    26     @BeforeClass
    27     public static void bfc(){
    28         System.out.println("before class");
    29     }
    30     
    31     @Before
    32     public void bef(){
    33         t=new test();
    34         System.out.println("before");
    35     }
    36     
    37     
    38     @Test
    39     public  void ts(){
    40         int result=t.add(1, 1);
    41         //期望得到的值与实际的结果相比较
    42         Assert.assertEquals(2, result);
    43         System.out.println("ts");
    44     }
    45     
    46     @Test
    47     public  void ts2(){
    48         int result=t.odd(1, 1);
    49         //期望得到的值与实际的结果相比较
    50         Assert.assertEquals(0, result);
    51         System.out.println("ts2");
    52     }
    53     
    54     
    55     @After
    56     public void aft(){
    57         t=null;
    58         System.out.println("after");
    59     }
    60     
    61     @AfterClass
    62     public static void afc(){
    63         System.out.println("after class");
    64     }
    65     
    66 }

    结果图:BeforeClass和AfterClass只执行一次,Before和After在每个Test执行的时候都会执行一次

     

  • 相关阅读:
    虹软人脸识别在 linux中so文件加载不到的问题
    tomcat 控制台乱码问题
    sourceTree git 空目录从远程仓库克隆代码出现warning: templates not found
    springboot项目更改代码后实时刷新问题
    spring 3.0 整合redis
    随笔
    Centos 7 安装 FFmpeg
    Postgresql 查看当前数据库所有的触发器
    oracle只导出触发器
    oracle 批量删除触发器
  • 原文地址:https://www.cnblogs.com/xujingyang/p/6421330.html
Copyright © 2020-2023  润新知