📜  JUnit-API

📅  最后修改于: 2020-11-12 04:38:05             🧑  作者: Mango


JUnit中最重要的软件包是junit.framework ,其中包含所有核心类。一些重要的类如下-

Sr.No. Class Name Functionality
1 Assert A set of assert methods.
2 TestCase A test case defines the fixture to run multiple tests.
3 TestResult A TestResult collects the results of executing a test case.
4 TestSuite A TestSuite is a composite of tests.

断言课

以下是org.junit.Assert类的声明-

public class Assert extends java.lang.Object

此类提供了一组断言方法,可用于编写测试。仅记录失败的断言。 Assert类的一些重要方法如下-

Sr.No. Methods & Description
1

void assertEquals(boolean expected, boolean actual)

Checks that two primitives/objects are equal.

2

void assertFalse(boolean condition)

Checks that a condition is false.

3

void assertNotNull(Object object)

Checks that an object isn’t null.

4

void assertNull(Object object)

Checks that an object is null.

5

void assertTrue(boolean condition)

Checks that a condition is true.

6

void fail()

Fails a test with no message.

让我们在示例中使用一些上述方法。在C:\> JUNIT_WORKSPACE中创建一个名为TestJunit1.java的Java类文件。

import org.junit.Test;
import static org.junit.Assert.*;

public class TestJunit1 {
   @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);

      //check for not null value
      assertNotNull(temp);
   }
}

接下来,在C:\> JUNIT_WORKSPACE中创建一个名为TestRunner1.java的Java类文件以执行测试用例。

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

public class TestRunner1 {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(TestJunit1.class);
        
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
        
      System.out.println(result.wasSuccessful());
   }
}      

使用javac编译测试用例和Test Runner类。

C:\JUNIT_WORKSPACE>javac TestJunit1.java TestRunner1.java

现在运行Test Runner,它将运行在提供的Test Case类中定义的测试用例。

C:\JUNIT_WORKSPACE>java TestRunner1

验证输出。

true

TestCase类

以下是org.junit.TestCase类的声明-

public abstract class TestCase extends Assert implements Test

测试用例定义了夹具以运行多个测试。 TestCase类的一些重要方法如下-

Sr.No. Methods & Description
1

int countTestCases()

Counts the number of test cases executed by run(TestResult result).

2

TestResult createResult()

Creates a default TestResult object.

3

String getName()

Gets the name of a TestCase.

4

TestResult run()

A convenience method to run this test, collecting the results with a default TestResult object.

5

void run(TestResult result)

Runs the test case and collects the results in TestResult.

6

void setName(String name)

Sets the name of a TestCase.

7

void setUp()

Sets up the fixture, for example, open a network connection.

8

void tearDown()

Tears down the fixture, for example, close a network connection.

9

String toString()

Returns a string representation of the test case.

让我们在示例中使用一些上述方法。在C:\> JUNIT_WORKSPACE中创建一个名为TestJunit2.java的Java类文件。

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

public class TestJunit2 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(  ) {
   }
}

接下来,在C:\> JUNIT_WORKSPACE中创建一个名为TestRunner2.java的Java类文件以执行测试用例。

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

public class TestRunner2 {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(TestJunit2.class);
        
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
        
      System.out.println(result.wasSuccessful());
   }
} 

使用javac编译测试用例和Test Runner类。

C:\JUNIT_WORKSPACE>javac TestJunit2.java TestRunner2.java

现在运行Test Runner,它将运行在提供的Test Case类中定义的测试用例。

C:\JUNIT_WORKSPACE>java TestRunner2

验证输出。

No of Test Case = 1
Test Case Name = testAdd
Updated Test Case Name = testNewAdd
true

TestResult类

以下是org.junit.TestResult类的声明-

public class TestResult extends Object

TestResult收集执行测试用例的结果。它是“收集参数”模式的一个实例。测试框架区分失败和错误。预计会发生故障,并使用断言检查故障。错误是意外的问题,例如ArrayIndexOutOfBoundsException。 TestResult类的一些重要方法如下-

Sr.No. Methods & Description
1

void addError(Test test, Throwable t)

Adds an error to the list of errors.

2

void addFailure(Test test, AssertionFailedError t)

Adds a failure to the list of failures.

3

void endTest(Test test)

Informs the result that a test was completed.

4

int errorCount()

Gets the number of detected errors.

5

Enumeration errors()

Returns an Enumeration for the errors.

6

int failureCount()

Gets the number of detected failures.

7

void run(TestCase test)

Runs a TestCase.

8

int runCount()

Gets the number of run tests.

9

void startTest(Test test)

Informs the result that a test will be started.

10

void stop()

Marks that the test run should stop.

在C:\> JUNIT_WORKSPACE中创建一个名为TestJunit3.java的Java类文件。

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

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

   // add the failure
   public synchronized void addFailure(Test test, AssertionFailedError t) {
      super.addFailure((junit.framework.Test) test, t);
   }
    
   @Test
   public void testAdd() {
      // add any test
   }
   
   // Marks that the test run should stop.
   public synchronized void stop() {
      //stop the test here
   }
}

接下来,在C:\> JUNIT_WORKSPACE中创建一个名为TestRunner3.java的Java类文件以执行测试用例。

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

public class TestRunner3 {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(TestJunit3.class);

      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
        
      System.out.println(result.wasSuccessful());
   }
}      

使用javac编译测试用例和Test Runner类。

C:\JUNIT_WORKSPACE>javac TestJunit3.java TestRunner3.java

现在运行Test Runner,它将运行在提供的Test Case类中定义的测试用例。

C:\JUNIT_WORKSPACE>java TestRunner3

验证输出。

true

TestSuite类

以下是org.junit.TestSuite类的声明:

public class TestSuite extends Object implements Test

TestSuite是测试的组合。它运行测试用例的集合。 TestSuite类的一些重要方法如下-

Sr.No. Methods & Description
1

void addTest(Test test)

Adds a test to the suite.

2

void addTestSuite(Class testClass)

Adds the tests from the given class to the suite.

3

int countTestCases()

Counts the number of test cases that will be run by this test.

4

String getName()

Returns the name of the suite.

5

void run(TestResult result)

Runs the tests and collects their result in a TestResult.

6

void setName(String name)

Sets the name of the suite.

7

Test testAt(int index)

Returns the test at the given index.

8

int testCount()

Returns the number of tests in this suite.

9

static Test warning(String message)

Returns a test which will fail and log a warning message.

在C:\> JUNIT_WORKSPACE中创建一个名为JunitTestSuite.java的Java类文件,以创建测试套件。

import junit.framework.*;

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

使用javac编译测试套件类。

C:\JUNIT_WORKSPACE>javac JunitTestSuite.java 

现在运行测试套件。

C:\JUNIT_WORKSPACE>java JunitTestSuite

验证输出。

No of Test Case = 1
Test Case Name = testAdd
Updated Test Case Name = testNewAdd
Number of test cases = 3