Junit

概述

junit(单元测试框架)

目前存在的问题:
    1. 目前的方法如果需要测试,都需要在main方法上调用。
    2. 目前的结果都需要我们人工对比。


junit要注意的细节:
    1. 如果使用junit测试一个方法的时候,在junit窗口上显示绿条那么代表测试正确,
        如果是出现了红条,则代表该方法测试出现了异常不通过。
    2. 如果点击方法名、 类名、包名、 工程名运行junit分别测试的是对应的方法,类、 包中 的所有类的test方法,工程中的所有test方法。
    3.  @Test测试的方法不能是static修饰与不能带有形参。
    4. 如果测试一个方法的时候需要准备测试的环境或者是清理测试的环境,那么可以@Before、 @After 、@BeforeClass、 @AfterClass这四个注解。
    @Before、 @After 是在每个测试方法测试的时候都会调用一次, @BeforeClass、 @AfterClass是在所有的测试方法测试之前与测试之后调用一次而已。

junit使用规范:
    1. 一个类如果需要测试,那么该类就应该对应着一个测试类,测试类的命名规范 : 被测试类的类名+ Test.
    2. 一个被测试的方法一般对应着一个测试的方法,测试的方法的命名规范是: test+ 被测试的方法的方法名
package cn.itcsat.junit;

import static org.junit.Assert.*;

import java.util.Arrays;

import javax.management.RuntimeErrorException;

import org.junit.Test;

public class Demo1 {

    @Test //注解
    public   void getMax(int a, int b){
    /*  int a = 3;
        int b = 5 ;*/
        int max = a>b?a:b;
        System.out.println("最大值:"+max);
    }


    @Test
    public void sort(){
        int[] arr = {12,4,1,19};
        for(int i = 0 ; i  < arr.length-1 ; i++){
            for(int j = i+1 ; j<arr.length ; j++){
                if(arr[i]>arr[j]){
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }

        System.out.println("数组的元素:"+Arrays.toString(arr));

    }


}
package cn.itcsat.junit;

import java.io.FileInputStream;
import java.io.IOException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class Demo2 {

    //准备测试的环境
    //@Before
    @BeforeClass
    public static void beforeRead(){
        System.out.println("准备测试环境成功...");
    }


    //读取文件数据,把把文件数据都
    @Test
    public void readFile() throws IOException{
        FileInputStream fileInputStream = new FileInputStream("F:\\a.txt");
        int content  = fileInputStream.read();
        System.out.println("内容:"+content);
    }


    @Test
    public void sort(){
        System.out.println("读取文件数据排序..");
    }


    //清理测试环境的方法
//  @After 
    @AfterClass
    public static void afterRead(){
        System.out.println("清理测试环境..");
    }


}

Assert

Assert.assertSame(5, max); // 底层使用 ==
Assert.assertSame(new String("abc"), "abc");
Assert.assertEquals(new String("abc"), "abc"); //底层是使用Equals方法比较的
Assert.assertNull("aa");
Assert.assertTrue(true);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值