Junit使用教程(二)
来源:互联网 发布:ubuntu安装时合并分区 编辑:程序博客网 时间:2024/06/05 12:43
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. 示例
- package test;
- import static org.hamcrest.CoreMatchers.*;
- import static org.junit.Assert.*;
- import java.util.Arrays;
- import org.hamcrest.core.CombinableMatcher;
- import org.junit.Test;
- public class AssertTests {
- @Test
- public void testAssertArrayEquals() {
- byte[] expected = "trial".getBytes();
- byte[] actual = "trial".getBytes();
- org.junit.Assert.assertArrayEquals("failure - byte arrays not same", expected, actual);
- }
- @Test
- public void testAssertEquals() {
- org.junit.Assert.assertEquals("failure - strings not same", 5l, 5l);
- }
- @Test
- public void testAssertFalse() {
- org.junit.Assert.assertFalse("failure - should be false", false);
- }
- @Test
- public void testAssertNotNull() {
- org.junit.Assert.assertNotNull("should not be null", new Object());
- }
- @Test
- public void testAssertNotSame() {
- org.junit.Assert.assertNotSame("should not be same Object", new Object(), new Object());
- }
- @Test
- public void testAssertNull() {
- org.junit.Assert.assertNull("should be null", null);
- }
- @Test
- public void testAssertSame() {
- Integer aNumber = Integer.valueOf(768);
- org.junit.Assert.assertSame("should be same", aNumber, aNumber);
- }
- // JUnit Matchers assertThat
- @Test
- public void testAssertThatBothContainsString() {
- org.junit.Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));
- }
- @Test
- public void testAssertThathasItemsContainsString() {
- org.junit.Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
- }
- @Test
- public void testAssertThatEveryItemContainsString() {
- org.junit.Assert.assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));
- }
- // Core Hamcrest Matchers with assertThat
- @Test
- public void testAssertThatHamcrestCoreMatchers() {
- assertThat("good", allOf(equalTo("good"), startsWith("good")));
- assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
- assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
- assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4))));
- assertThat(new Object(), not(sameInstance(new Object())));
- }
- }
三、核心——注解
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. 示例
- package test;
- import static org.junit.Assert.*;
- import org.junit.*;
- public class JDemoTest {
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- System.out.println("in BeforeClass================");
- }
- @AfterClass
- public static void tearDownAfterClass() throws Exception {
- System.out.println("in AfterClass=================");
- }
- @Before
- public void before() {
- System.out.println("in Before");
- }
- @After
- public void after() {
- System.out.println("in After");
- }
- @Test(timeout = 10000)
- public void testadd() {
- JDemo a = new JDemo();
- assertEquals(6, a.add(3, 3));
- System.out.println("in Test ----Add");
- }
- @Test
- public void testdivision() {
- JDemo a = new JDemo();
- assertEquals(3, a.division(6, 2));
- System.out.println("in Test ----Division");
- }
- @Ignore
- @Test
- public void test_ignore() {
- JDemo a = new JDemo();
- assertEquals(6, a.add(1, 5));
- System.out.println("in test_ignore");
- }
- @Test
- public void teest_fail() {
- fail();
- }
- }
- class JDemo extends Thread {
- int result;
- public int add(int a, int b) {
- try {
- sleep(1000);
- result = a + b;
- } catch (InterruptedException e) {
- }
- return result;
- }
- public int division(int a, int b) {
- return result = a / b;
- }
- }
执行结果:
- in BeforeClass================
- in Before
- in Test ----Add
- in After
- in Before
- in Test ----Division
- in After
- in AfterClass=================
图中左上红框中部分表示Junit运行结果,5个成功(1个忽略),1个错误,1个失败。(注意错误和失败不是一回事,错误说明代码有错误,而失败表示该测试方法测试失败)
左下红框中则表示出了各个测试方法的运行状态,可以看到成功、错误、失败、失败各自的图标是不一样的,还可以看到运行时间。
右边部分则是异常堆栈,可查看异常信息。
下篇中我们给出更多示例还继续介绍Junit
0 0
- 【Junit】Junit使用教程(二)
- Junit使用教程(二)
- Junit使用教程(二)
- Junit使用教程(二)
- Junit使用教程(二)
- Junit使用教程(二)
- Junit使用教程(二)
- Junit使用教程(二)
- Junit使用教程(二)
- Junit使用教程(二)
- 【Junit】Junit使用教程(一)
- 【Junit】Junit使用教程(三)
- 【Junit】Junit使用教程(四)
- Junit使用教程(一)
- Junit使用教程(三)
- Junit使用教程(四)
- Junit使用教程(三) .
- Junit使用教程(四) .
- ubuntu14.04下安装gcc-4.7 笔记
- ADB常用指令与logcat日志
- yii 框架中的上传 分页
- windows下彻底删除Oracle
- 关于eclipse与maven的集成
- Junit使用教程(二)
- Strategy 策略模式 在 Spring 开发中的应用 包括事务管理,日志管理(logger),IoC容器根据bean定义的内容实例化等
- Hibernate基础知识(一)
- 你好,世界!
- javaBean之简介、设计原理、动作元素
- caffe中使用crop_size剪裁训练图片
- 【JZOJ4800】周末晚会 题解
- C语言中关于float、double、long double精度及数值范围理解
- UiAutomator UiWatcher