静下心来的个人专栏
上一篇

Assertion使用

广告
选中文字可对指定文章内容进行评论啦,→和←可快速切换按钮,绿色背景文字可以点击查看评论额。
大纲

Assertion

所有断言都在 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 assertTrue(boolean condition)

Checks that a condition is true.

3

void assertFalse(boolean condition)

Checks that a condition is false.

4

void assertNotNull(Object object)

Checks that an object isn't null.

5

void assertNull(Object object)

Checks that an object is null.

6

void assertSame(object1, object2)

The assertSame() method tests if two object references point to the same object.

7

void assertNotSame(object1, object2)

The assertNotSame() method tests if two object references do not point to the same object.

8

void assertArrayEquals(expectedArray, resultArray);

The assertArrayEquals() method will test whether two arrays are equal to each other.

让我们在一个例子中使用上面提到的一些方法。创建一个名为 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);
   }
}

接下来,创建一个名为 TestRunner.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(TestAssertions.class);
		
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
		
      System.out.println(result.wasSuccessful());
   }
} 

Annotation

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

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

哪些方法在所有方法之前和之后运行。

以及在执行过程中将忽略哪些方法或类。

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

Sr.No.Annotation & Description
1

@Test

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case.

2

@Before

Several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before each Test method.

3

@After

If you allocate external resources in a Before method, you need to release them after the test runs. Annotating a public void method with @After causes that method to be run after the Test method.

4

@BeforeClass

Annotating a public static void method with @BeforeClass causes it to be run once before any of the test methods in the class.

5

@AfterClass

This will perform the method after all tests have finished. This can be used to perform clean-up activities.

6

@Ignore

The Ignore annotation is used to ignore the test and that test will not be executed.

创建一个名为 JunitAnnotation.java 的 java 类文件

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");
   }
}

接下来,创建一个名为 TestRunner.java 的 java 类文件

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());
   }
} 

输出如下:

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

版权声明:著作权归作者所有。

X

欢迎加群学习交流

联系我们