前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Junit使用教程(二)

Junit使用教程(二)

作者头像
bear_fish
发布2018-09-19 15:16:44
1K0
发布2018-09-19 15:16:44
举报
文章被收录于专栏:用户2442861的专栏

http://blog.csdn.net/wangpeng047/article/details/9628449

二、核心——断言

断言是编写测试用例的核心实现方式,即期望值是多少,测试的结果是多少,以此来判断测试是否通过。

1. 断言核心方法

assertArrayEquals(expecteds, actuals)

查看两个数组是否相等。

assertEquals(expected, actual)

查看两个对象是否相等。类似于字符串比较使用的equals()方法

assertNotEquals(first, second)

查看两个对象是否不相等。

assertNull(object)

查看对象是否为空。

assertNotNull(object)

查看对象是否不为空。

assertSame(expected, actual)

查看两个对象的引用是否相等。类似于使用“==”比较两个对象

assertNotSame(unexpected, actual)

查看两个对象的引用是否不相等。类似于使用“!=”比较两个对象

assertTrue(condition)

查看运行结果是否为true。

assertFalse(condition)

查看运行结果是否为false。

assertThat(actual, matcher)

查看实际值是否满足指定的条件

fail()

让测试失败

2. 示例

[java] view plain copy

  1. package test;  
  2. import static org.hamcrest.CoreMatchers.*;  
  3. import static org.junit.Assert.*;  
  4. import java.util.Arrays;  
  5. import org.hamcrest.core.CombinableMatcher;  
  6. import org.junit.Test;  
  7. public class AssertTests {  
  8. @Test
  9. public void testAssertArrayEquals() {  
  10. byte[] expected = "trial".getBytes();  
  11. byte[] actual = "trial".getBytes();  
  12.         org.junit.Assert.assertArrayEquals("failure - byte arrays not same", expected, actual);  
  13.       }  
  14. @Test
  15. public void testAssertEquals() {  
  16.         org.junit.Assert.assertEquals("failure - strings not same", 5l, 5l);  
  17.       }  
  18. @Test
  19. public void testAssertFalse() {  
  20.         org.junit.Assert.assertFalse("failure - should be false", false);  
  21.       }  
  22. @Test
  23. public void testAssertNotNull() {  
  24.         org.junit.Assert.assertNotNull("should not be null", new Object());  
  25.       }  
  26. @Test
  27. public void testAssertNotSame() {  
  28.         org.junit.Assert.assertNotSame("should not be same Object", new Object(), new Object());  
  29.       }  
  30. @Test
  31. public void testAssertNull() {  
  32.         org.junit.Assert.assertNull("should be null", null);  
  33.       }  
  34. @Test
  35. public void testAssertSame() {  
  36.         Integer aNumber = Integer.valueOf(768);  
  37.         org.junit.Assert.assertSame("should be same", aNumber, aNumber);  
  38.       }  
  39. // JUnit Matchers assertThat
  40. @Test
  41. public void testAssertThatBothContainsString() {  
  42.         org.junit.Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));  
  43.       }  
  44. @Test
  45. public void testAssertThathasItemsContainsString() {  
  46.         org.junit.Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));  
  47.       }  
  48. @Test
  49. public void testAssertThatEveryItemContainsString() {  
  50.         org.junit.Assert.assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));  
  51.       }  
  52. // Core Hamcrest Matchers with assertThat
  53. @Test
  54. public void testAssertThatHamcrestCoreMatchers() {  
  55.         assertThat("good", allOf(equalTo("good"), startsWith("good")));  
  56.         assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));  
  57.         assertThat("good", anyOf(equalTo("bad"), equalTo("good")));  
  58.         assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4))));  
  59.         assertThat(new Object(), not(sameInstance(new Object())));  
  60.       }  
  61. }  

三、核心——注解

1. 说明

@Before

初始化方法

@After

释放资源

@Test

测试方法,在这里可以测试期望异常和超时时间

@Ignore

忽略的测试方法

@BeforeClass

针对所有测试,只执行一次,且必须为static void

@AfterClass

针对所有测试,只执行一次,且必须为static void

@RunWith

指定测试类使用某个运行器

@Parameters

指定测试类的测试数据集合

@Rule

允许灵活添加或重新定义测试类中的每个测试方法的行为

@FixMethodOrder

指定测试方法的执行顺序

2. 执行顺序

一个测试类单元测试的执行顺序为:

@BeforeClass –> @Before –> @Test –> @After –> @AfterClass

每一个测试方法的调用顺序为:

@Before –> @Test –> @After

3. 示例

[java] view plain copy

  1. package test;  
  2. import static org.junit.Assert.*;  
  3. import org.junit.*;  
  4. public class JDemoTest {  
  5. @BeforeClass
  6. public static void setUpBeforeClass() throws Exception {  
  7.         System.out.println("in BeforeClass================");  
  8.     }  
  9. @AfterClass
  10. public static void tearDownAfterClass() throws Exception {  
  11.         System.out.println("in AfterClass=================");  
  12.     }  
  13. @Before
  14. public void before() {  
  15.         System.out.println("in Before");  
  16.     }  
  17. @After
  18. public void after() {  
  19.         System.out.println("in After");  
  20.     }  
  21. @Test(timeout = 10000)  
  22. public void testadd() {  
  23.         JDemo a = new JDemo();  
  24.         assertEquals(6, a.add(3, 3));  
  25.         System.out.println("in Test ----Add");  
  26.     }  
  27. @Test
  28. public void testdivision() {  
  29.         JDemo a = new JDemo();  
  30.         assertEquals(3, a.division(6, 2));  
  31.         System.out.println("in Test ----Division");  
  32.     }  
  33. @Ignore
  34. @Test
  35. public void test_ignore() {  
  36.         JDemo a = new JDemo();  
  37.         assertEquals(6, a.add(1, 5));  
  38.         System.out.println("in test_ignore");  
  39.     }  
  40. @Test
  41. public void teest_fail() {  
  42.         fail();  
  43.     }  
  44. }  
  45. class JDemo extends Thread {  
  46. int result;  
  47. public int add(int a, int b) {  
  48. try {  
  49.             sleep(1000);  
  50.             result = a + b;  
  51.         } catch (InterruptedException e) {  
  52.         }  
  53. return result;  
  54.     }  
  55. public int division(int a, int b) {  
  56. return result = a / b;  
  57.     }  
  58. }  

执行结果:

[plain] view plain copy

  1. in BeforeClass================  
  2. in Before  
  3. in Test ----Add  
  4. in After  
  5. in Before  
  6. in Test ----Division  
  7. in After  
  8. in AfterClass=================  

图中左上红框中部分表示Junit运行结果,5个成功(1个忽略),1个错误,1个失败。(注意错误和失败不是一回事,错误说明代码有错误,而失败表示该测试方法测试失败)

左下红框中则表示出了各个测试方法的运行状态,可以看到成功、错误、失败、失败各自的图标是不一样的,还可以看到运行时间。

右边部分则是异常堆栈,可查看异常信息。

下篇中我们给出更多示例还继续介绍Junit

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年09月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档