深入探索:单元测试之Assertions


声明:本文转载自https://my.oschina.net/u/1187675/blog/1533728,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

assert 简述

JUnit 为全部的基本类型、对象 以及数组(包括基本类型和对象),参数顺序是期望值,后面跟随的是实际结果值。第一个参数是可选的,是在测试失败的时候输出的字符串信息。此外,这里还有一个不同于Assert 的,那就是assertThat, 它具有可选的失败消息参数实际值,和一个匹配对象。这里有一点需要注意的,那就是参数,assertThat 的参数是与其他assert 的参数是反过来的,比如assertEquals("failure - strings are not equal", "期望值", "实际结果值"); assertThat("实际结果值", 期望匹配值);

举例

使用Junit 需要一些jar 包,maven 的依赖如下:

		<dependency> 			<groupId>junit</groupId> 			<artifactId>junit</artifactId> 			<version>4.12</version> 			<scope>test</scope> 		</dependency> 		<dependency> 		    <groupId>org.hamcrest</groupId> 		    <artifactId>hamcrest-all</artifactId> 		    <version>1.3</version> 		    <scope>test</scope> 		</dependency> 

下面提供一些比较有代表性的assert例子:

import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.CoreMatchers.both; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.everyItem; import static org.hamcrest.CoreMatchers.hasItems; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue;  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();     assertArrayEquals("failure - byte arrays not same", expected, actual);   }    @Test   public void testAssertEquals() {     assertEquals("failure - strings are not equal", "text", "text");   }    @Test   public void testAssertFalse() {     assertFalse("failure - should be false", false);   }    @Test   public void testAssertNotNull() {     assertNotNull("should not be null", new Object());   }    @Test   public void testAssertNotSame() {     assertNotSame("should not be same Object", new Object(), new Object());   }    @Test   public void testAssertNull() {     assertNull("should be null", null);   }    @Test   public void testAssertSame() {     Integer aNumber = Integer.valueOf(768);     assertSame("should be same", aNumber, aNumber);   }    // JUnit Matchers assertThat   @Test   public void testAssertThatBothContainsString() {     assertThat("albumen", both(containsString("a")).and(containsString("b")));   }    @Test   public void testAssertThatHasItems() {     assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));   }    @Test   public void testAssertThatEveryItemContainsString() {     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())));   }    @Test   public void testAssertTrue() {     assertTrue("failure - should be true", true);   } } 

本文发表于2017年09月10日 18:36
(c)注:本文转载自https://my.oschina.net/u/1187675/blog/1533728,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1871 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1