教程 > junit 教程 > 阅读:23

junit 使用断言——迹忆客-ag捕鱼王app官网

断言

所有的断言都包含在 assert 类中

public class assert extends java.lang.object

这个类提供了很多有用的断言方法来编写测试用例。只有失败的断言才会被记录。assert 类中的一些有用的方法列式如下:

序号 方法 描述
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(object expected, object actual) assertsame() 方法检查两个相关对象是否指向同一个对象
7 void assertnotsame(object expected, object actual) assertnotsame() 方法检查两个相关对象是否不指向同一个对象
8 void assertarrayequals(expectedarray, resultarray) assertarrayequals() 方法检查两个数组是否相等

下面我们在例子中试验一下上面提到的各种方法。在 c:\ > junit_workspace 路径下创建一个文件名为 testassertions.java 的类

testassertions.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:\ > junit_workspace 路径下创建一个文件名为 testrunner.java 的类来执行测试用例

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

用 javac 编译 test casetest runner

c:\junit_workspace>javac testassertions.java testrunner.java

现在运行将会运行 test case 类中定义和提供的测试案例的 test runner

c:\junit_workspace>java testrunner

检查运行结果

true

注解

注解就好像你可以在你的代码中添加并且在方法或者类中应用的元标签。junit 中的这些注解为我们提供了测试方法的相关信息,哪些方法将会在测试方法前后应用,哪些方法将会在所有方法前后应用,哪些方法将会在执行中被忽略。

junit 中的注解的列表以及他们的含义:

序号 注解 描述
1 @test 这个注解说明依附在 junit 的 public void 方法可以作为一个测试案例。
2 @before 有些测试在运行前需要创造几个相似的对象。在 public void 方法加该注解是因为该方法需要在 test 方法前运行。
3 @after 如果你将外部资源在 before 方法中分配,那么你需要在测试运行后释放他们。在 public void 方法加该注解是因为该方法需要在 test 方法后运行。
4 @beforeclass 在 public void 方法加该注解是因为该方法需要在类中所有方法前运行。
5 @afterclass 它将会使方法在所有测试结束后执行。这个可以用来进行清理活动。
6 @ignore 这个注解是用来忽略有关不需要执行的测试的。

c:\ > junit_workspace 路径下创建一个文件名为 junitannotation.java 的类来测试注解

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

接下来,我们在 c:\ > junit_workspace 路径下创建一个文件名为 testrunner.java 的类来执行注解

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

用 javac 编译 test casetest runner

c:\junit_workspace>javac testassertions.java testrunner.java

现在运行将会运行 test case 类中定义和提供的测试案例的 test runner

c:\junit_workspace>java testrunner

检查运行结果

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

查看笔记

扫码一下
查看教程更方便
网站地图