Junit | Junit 学习笔记

Assert 类, 断言

org.junit.Assert 类
	public class Assert extends java.lang.Object
这个类提供了一系列的编写测试的有用的声明方法. 只有失败的声明方法才会被记录.

在这里插入图片描述

assertFalse()

比如 assertFalse 的条件是假的, 则返回 true
代码:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class AssertTest {
    @Test
    public void testAdd(){
        //test data
        int num = 5;
        String temp = null;
        String str = "JUnit is working fine";

        // check for equality
        //assertEquals("Junit is working fine",str);

        // check for false condition
        assertFalse(num > 6);
    }
}

该结果返回 true, 如果条件改为 num == 5, 则返回 false, 并且会有一些错误信息.

assertEquals()

public static void assertEquals(long expected, long actual)

Asserts that two longs are equal. If they are not, an AssertionError is thrown.----------断言两个 long 类型是相等的. 如果不是, 则抛出断言错误.

public static void assertEquals(String message, long expected, long actual)

断言两个 long 类型相等. 如果不是, 则用给定的消息抛出断言错误.

断言练习

import static org.junit.Assert.*;

public class TestAssertions {
    @Test
    public void testAssertions(){
        // test data
        String str1 = new String("abc");
        String str2 = new String("abc");
        String str3 = null;
        String str4 = "abc";
        String str5 = "abc";
        int val1 = 5;
        int val2 = 6;
        String[] expectedArray = {"one","two","three"};
        String[] resultArray = {"one","two","three"};

        // Check that two objects are equal
        assertEquals(str1, str2);

        // Check that a condition is true
        assertTrue(val1 < val2);

        // Check that a condition is false
        assertFalse(val1 > val2);

        // Check that an object isn't null
        assertNotNull(str1);

        // Check that an object is null
        assertNull(str3);

        // Check if two object references point to the same object
        assertSame(str4, str5);

        // Check if two object references not point to the same object
        assertNotSame(str1, str3);

        // Check whether two arrays are equal to each other.
        assertArrayEquals(expectedArray, resultArray);
    }
}

TestCase() 类

在这里插入图片描述
了解到:
setUp() 主要实现测试前的初始化工作
tearDown()主要实现测试完成后的垃圾回收等工作

练习

import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;

public class TestCaseTest extends TestCase {
    protected double fValue1;
    protected double fValue2;

    @Before
    public void setUp(){
        fValue1 = 2.0;
        fValue2 = 3.0;
    }

    @Test
    public void testAdd(){
        //count the number of test cases
        System.out.println("No of Test Case = "+ this.countTestCases());

        //test getName
        String name = this.getName();
        System.out.println("Test Case Name = "+ name);

        //test setName
        this.setName("testNewAdd");
        String newName = this.getName();
        System.out.println("Updated Test Case Name = "+ newName);
    }

    //tearDown used to close the connection or clean up activities
    public void tearDown(){
    }
}

TestResult 类—不理解

在这里插入图片描述

练习

import junit.framework.AssertionFailedError;
import junit.framework.TestResult;
import org.junit.Test;

import static org.junit.Assert.assertFalse;

public class TestResultTest extends TestResult {
    //add the error
    public synchronized void addError(Test test, Throwable t){
        super.addError((junit.framework.Test) test, t);
        System.out.println(t);
    }

    // add the failure
    public synchronized void addFailure(Test test, AssertionFailedError t){
        super.addFailure((junit.framework.Test)test, t);
        System.out.println(t);
    }

    @Test
    public void testAdd(){
        //add any test

        //test data
        int num = 5;
        String temp = null;
        String str = "JUnit is working fine";

        // check for equality
        //assertEquals("Junit is working fine",str);

        // check for false condition
        //assertFalse(num == 5);
    }

    // Marks that the test run should stop.
    public synchronized void stop(){
        // stop the test here
    }
}

不能理解 addError 和 addFailure 是具体用来干什么的, 因为testAdd里面加了前面 Assert 类测试的错误, 但是并不会输出东西.

上网找到的资料:
Failure指的是预期的结果与实际运行单元的结果不同所导致,例如当您使用assertEquals()或其它assertXXX()方法断言失败时,就会回报Failure,这时候您要检查您的单元方法中的逻辑设计是否有误。

Error指的是您程式没有考虑到的情况,在断言之前程式就因为某种错误引发例外而终止,例如在单元中存取某个阵列,因为存取超出索引而引发ArrayIndexOutOfBoundsException,这会使得单元方法无法正确完成,在测试运行到asertXXXX()前就提前结束,这时候您要检查您的单元方法中是否有未考虑到的情况而引发流程突然中断。

TestSuite 类

在这里插入图片描述

练习

public class TestSuiteTest {
    public static void main(String[] args) {
        // add the test's in the suite
        TestSuite suite = new TestSuite(AssertTest.class, TestResultTest.class);
        TestResult result = new TestResult();
        suite.run(result);
        System.out.println("Number of test cases = " + result.runCount());
    }
}

结果
在这里插入图片描述

TestRunner 类

每个测试用例开始跑都会用到的类

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
    public static void main(String[] args) {
        Result result = JUnitCore.runClasses(TestJunit.class);
        for (Failure failure:result.getFailures()){
            System.out.println(failure.toString());
        }
        System.out.println(result.wasSuccessful());
    }
}
©️2020 CSDN 皮肤主题: 像素格子 设计师:CSDN官方博客 返回首页