课程表

JUnit课程

工具箱
速查手册

JUnit - 使用断言

当前位置:免费教程 » Java相关 » JUnit

断言

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

  1. public class Assert extends java.lang.Object

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

序号 方法和描述
1 void assertEquals(boolean expected, boolean actual)
检查两个变量或者等式是否平衡
2 void assertTrue(boolean expected, boolean actual)
检查条件为真
3 void assertFalse(boolean condition)
检查条件为假
4 void assertNotNull(Object object)
检查对象不为空
5 void assertNull(Object object)
检查对象为空
6 void assertSame(boolean condition)
assertSame() 方法检查两个相关对象是否指向同一个对象
7 void assertNotSame(boolean condition)
assertNotSame() 方法检查两个相关对象是否不指向同一个对象
8 void assertArrayEquals(expectedArray, resultArray)
assertArrayEquals() 方法检查两个数组是否相等

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

  1. import org.junit.Test;
  2. import static org.junit.Assert.*;
  3. public class TestAssertions {
  4. @Test
  5. public void testAssertions() {
  6. //test data
  7. String str1 = new String ("abc");
  8. String str2 = new String ("abc");
  9. String str3 = null;
  10. String str4 = "abc";
  11. String str5 = "abc";
  12. int val1 = 5;
  13. int val2 = 6;
  14. String[] expectedArray = {"one", "two", "three"};
  15. String[] resultArray = {"one", "two", "three"};
  16. //Check that two objects are equal
  17. assertEquals(str1, str2);
  18. //Check that a condition is true
  19. assertTrue (val1 < val2);
  20. //Check that a condition is false
  21. assertFalse(val1 > val2);
  22. //Check that an object isn't null
  23. assertNotNull(str1);
  24. //Check that an object is null
  25. assertNull(str3);
  26. //Check if two object references point to the same object
  27. assertSame(str4,str5);
  28. //Check if two object references not point to the same object
  29. assertNotSame(str1,str3);
  30. //Check whether two arrays are equal to each other.
  31. assertArrayEquals(expectedArray, resultArray);
  32. }
  33. }

接下来,我们在 C:\ > JUNIT_WORKSPACE 路径下创建一个文件名为 TestRunner.java 的类来执行测试用例

  1. import org.junit.runner.JUnitCore;
  2. import org.junit.runner.Result;
  3. import org.junit.runner.notification.Failure;
  4. public class TestRunner2 {
  5. public static void main(String[] args) {
  6. Result result = JUnitCore.runClasses(TestAssertions.class);
  7. for (Failure failure : result.getFailures()) {
  8. System.out.println(failure.toString());
  9. }
  10. System.out.println(result.wasSuccessful());
  11. }
  12. }

用 javac 编译 Test case 和 Test Runner 类

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

现在运行将会运行 Test Case 类中定义和提供的测试案例的 Test Runner

  1. C:\JUNIT_WORKSPACE>java TestRunner

检查运行结果

  1. 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 的类来测试注释

  1. import org.junit.After;
  2. import org.junit.AfterClass;
  3. import org.junit.Before;
  4. import org.junit.BeforeClass;
  5. import org.junit.Ignore;
  6. import org.junit.Test;
  7. public class JunitAnnotation {
  8. //execute before class
  9. @BeforeClass
  10. public static void beforeClass() {
  11. System.out.println("in before class");
  12. }
  13. //execute after class
  14. @AfterClass
  15. public static void afterClass() {
  16. System.out.println("in after class");
  17. }
  18. //execute before test
  19. @Before
  20. public void before() {
  21. System.out.println("in before");
  22. }
  23. //execute after test
  24. @After
  25. public void after() {
  26. System.out.println("in after");
  27. }
  28. //test case
  29. @Test
  30. public void test() {
  31. System.out.println("in test");
  32. }
  33. //test case ignore and will not execute
  34. @Ignore
  35. public void ignoreTest() {
  36. System.out.println("in ignore test");
  37. }
  38. }

接下来,我们在 C:\ > JUNIT_WORKSPACE 路径下创建一个文件名为 TestRunner.java 的类来执行注释

  1. import org.junit.runner.JUnitCore;
  2. import org.junit.runner.Result;
  3. import org.junit.runner.notification.Failure;
  4. public class TestRunner {
  5. public static void main(String[] args) {
  6. Result result = JUnitCore.runClasses(JunitAnnotation.class);
  7. for (Failure failure : result.getFailures()) {
  8. System.out.println(failure.toString());
  9. }
  10. System.out.println(result.wasSuccessful());
  11. }
  12. }

用 javac 编译 Test case 和 Test Runner 类

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

现在运行将会运行 Test Case 类中定义和提供的测试案例的 Test Runner

  1. C:\JUNIT_WORKSPACE>java TestRunner

检查运行结果

  1. in before class
  2. in before
  3. in test
  4. in after
  5. in after class
  6. true
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号