JUnit - 使用断言

断言

所有断言都在Assert类中.

public class Assert extends java .lang.Object

该类提供了一组断言方法,对编写测试很有用.仅记录失败的断言. Assert类的一些重要方法如下:<

Sr.No.方法&描述
1

void assertEquals(boolean expected,boolean actual)

检查两个基元/对象是否相等.

2

void assertTrue(boolean condition)

检查条件是否为真.

3

void assertFalse(boolean condition)

检查条件是否为假.

4

void assertNotNull(Object object)

检查对象是否为空.

5

void assertNull(Object object)

检查对象是否为空.

6

void assertSame(object1,object2)

assertSame()方法测试两个对象引用是否指向同一个对象.

7

void assertNotSame(object1,object2)

assertNotSame()方法测试两个对象引用是否指向相同对象.

8

void assertArrayEquals(expectedArray,resultArray);

assertArrayEquals()方法将测试是否有两个数组彼此相等.

让我们使用上面提到的一些示例中的方法.在C:\> JUNIT_WORKSPACE中创建一个名为 TestAssertions.java 的java类文件.

import org.junit.Test;
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);
   }
}

接下来,在C:\\中创建一个名为 TestRunner.java 的java类文件\\&; JUNIT_WORKSPACE执行测试用例.

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(TestAssertions.class);
		
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
		
      System.out.println(result.wasSuccessful());
   }
}

使用javac编译测试用例和测试运行器类.

C:\ JUNIT_WORKSPACE> javac TestAssertions.java TestRunner.java

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

C:\ _JUNIT_WORKSPACE> java TestRunner

验证输出.

 
 true

注释

注释就像元标记,您可以将其添加到代码中,并将它们应用于方法或类中. JUnit中的这些注释提供了有关测试方法的以下信息 :

  • 哪些方法将在测试方法之前和之后运行.

  • 在所有方法之前和之后运行哪些方法,以及.

  • 在执行期间将忽略哪些方法或类.

下表提供了注释列表及其在JUnit : 中的含义;

Sr.No.注释&描述
1

@Test

测试注释告诉JUnit它附加的公共void方法可以作为测试用例运行.

2

@Before

多个测试需要在运行之前创建类似的对象.使用@Before注释公共void方法会导致该方法在每个Test方法之前运行.

3

@After

如果你在Before方法中分配外部资源,您需要在测试运行后释放它们.使用@After注释public void方法会导致该方法在Test方法之后运行.

4

@BeforeClass

注释a使用@BeforeClass的public static void方法使它在类中的任何测试方法之前运行一次.

5

@AfterClass

这将在所有测试完成后执行该方法.这可用于执行清理活动.

6

@Ignore

Ignore注释用于忽略测试和该测试不会被执行.

创建一个名为

import org.junit.After;
import org.junit.AfterClass;

import org.junit.Before;
import org.junit.BeforeClass;

import org.junit.Ignore;
import org.junit.Test;

public class JunitAnnotation {
	
   //execute before class
   @BeforeClass
   public static void beforeClass() {
      System.out.println("in before class");
   }

   //execute after class
   @AfterClass
   public static void  afterClass() {
      System.out.println("in after class");
   }

   //execute before test
   @Before
   public void before() {
      System.out.println("in before");
   }
	
   //execute after test
   @After
   public void after() {
      System.out.println("in after");
   }
	
   //test case
   @Test
   public void test() {
      System.out.println("in test");
   }
	
   //test case ignore and will not execute
   @Ignore
   public void ignoreTest() {
      System.out.println("in ignore test");
   }
}

接下来,在C:\\中创建一个名为 TestRunner.java 的java类文件\\&; JUNIT_WORKSPACE执行注释.

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(JunitAnnotation.class);
		
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
		
      System.out.println(result.wasSuccessful());
   }
}

使用javac编译测试用例和测试运行器类.

 
 C:\ _JUNIT_WORKSPACE> javac JunitAnnotation.java TestRunner.java

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

 
 C:\ _JUNIT_WORKSPACE> java TestRunner

验证输出.

in before class
in before
in test
in after
in after class
true