Junit使用及其原理分析

发表于:2017-1-24 10:45

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:alighters    来源:51Testing软件测试网采编

  引入
  在 build.gradle 文件中
  dependencies {
  testCompile 'junit:junit:4.12'
  }
  这其中会引入两个jar:junit-4.12.jar 和 hamcrest-core-1.3.jar
  介绍
  junit 中两个重要的类 Assume 和 Assert, 以及其他一些重要的注解:BeforeClass,AfterClass,After,Before 及 Test,Ignore。
  其中,BeforeClass 和 AfterClass 在每个类加载的开始和结束时运行,需要设置 static 方法;而 Before和After 则是在每个测试方法的开始之前和结束之后运行。
  在 hamcrest-core 的 jar 包中,在 org.hamcrest.core 包中提供了一系列操作运算封装,使测试代码更加地易读。如is,not,allOf,anyOf等。
  代码示例
@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);
}
@Test
public void testAssertTrue() {
assertTrue("failure - should be true", true);
}
  以上代码来自官方介绍的 Demo , 列举的是常用而又基础的操作,但遇到复杂的集合判断操作,就力不从心了,不过 Junit 提供了另一更为强大的 assertThat 方法,首先来看看它的使用:
//JUnitMatchersassertThat
@Test
publicvoidtestAssertThatBothContainsString(){
assertThat("albumen",both(containsString("a")).and(containsString("b")));
}
@Test
publicvoidtestAssertThatHasItems(){
assertThat(Arrays.asList("one","two","three"),hasItems("one","three"));
}
@Test
publicvoidtestAssertThatEveryItemContainsString(){
assertThat(Arrays.asList(newString[]{"fun","ban","net"}),everyItem(containsString("n")));
}
//CoreHamcrestMatcherswithassertThat
@Test
publicvoidtestAssertThatHamcrestCoreMatchers(){
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(newObject(),not(sameInstance(newObject())));
}
  这里的 assertThat 用了两种方法:一个是 JunitMatchers ,另一个就是 hamcrest matchers 的 assertThat,不过后者提供的功能相当强大,前者的方法已经标为废弃了。
  另外,官方也提及了其它第三方提供的 Matchers 实现:
  Excel spreadsheet matchers
  JSON matchers
  XML/XPath matchers
  所以再次我们只看后者,可以看出来的是其方法的最后一个参数非常灵活,紧接着我们看看其怎么实现的?
  assertThat 方法实现
public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
assertThat("", actual, matcher);
}
public static <T> void assertThat(String reason, T actual,
Matcher<? super T> matcher) {
MatcherAssert.assertThat(reason, actual, matcher);
}
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号