【Java开发技术之程序测试】Junit4 新功能学习总结

本文详细介绍了JUnit4的基本用法、测试方法总结以及如何利用Hamcrest进行更强大的断言操作。通过实例展示了如何使用各种Hamcrest匹配器进行测试,并提供了测试代码行覆盖率和分支覆盖率的统计方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Junit4 标注总结

image

2.测试方法总结

image

3.assertThat语句的基本使用

该语句是代替原来的断言语句,以一种可读性更强的形式呈现断言。

使用时,注意导入:(Eclipse某些版本不能自动导入)

import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.*;

常用语句如下:

allOf语句表示所有在括号内的测试都必须通过,该断言才成立。 any语句表示结果是某一个类及其子类的类型。 anyOf语句表示只要在括号内的任意一个语句成立,该断言则通过。 equalTo语句表示两者在数值上“相等”。 is语句在大多数时候表示一种判断的动作,例如上述几个例子。但在传入一个"XXX.class"这样的参数时,也可以代替instanceOf;另外,还可以替代equalTo。 not语句是“取反”。 notNullValue语句是判断不是null,与nullValue语句相反。 sameInstance语句表示"==",即是否指向同一个对象实例。

另外,其他常用语句如下:

assertThat(testedString, equalToIgnoringCase(expectedString)); 
/**equalToIgnoringWhiteSpace匹配符断言被测的字符串testedString 
*在忽略头尾的任意个空格的情况下等于expectedString, 
*注意:字符串中的空格不能被忽略 
*/ 
assertThat(testedString, equalToIgnoringWhiteSpace(expectedString); 
/**containsString匹配符断言被测的字符串testedString包含子字符串subString**/ 
assertThat(testedString, containsString(subString) ); 
/**endsWith匹配符断言被测的字符串testedString以子字符串suffix结尾*/ 
assertThat(testedString, endsWith(suffix)); 
/**startsWith匹配符断言被测的字符串testedString以子字符串prefix开始*/ 
assertThat(testedString, startsWith(prefix)); 
/**closeTo匹配符断言被测的浮点型数testedDouble在20.0¡À0.5范围之内*/ 
assertThat(testedDouble, closeTo( 20.0, 0.5 )); 
/**greaterThan匹配符断言被测的数值testedNumber大于16.0*/ 
assertThat(testedNumber, greaterThan(16.0)); 
/** lessThan匹配符断言被测的数值testedNumber小于16.0*/ 
assertThat(testedNumber, lessThan (16.0)); 
/** greaterThanOrEqualTo匹配符断言被测的数值testedNumber大于等于16.0*/ 
assertThat(testedNumber, greaterThanOrEqualTo (16.0)); 
/** lessThanOrEqualTo匹配符断言被测的testedNumber小于等于16.0*/ 
assertThat(testedNumber, lessThanOrEqualTo (16.0)); 
/**hasEntry匹配符断言被测的Map对象mapObject含有一个键值为"key"对应元素值为"value"的Entry项*/ 
assertThat(mapObject, hasEntry("key", "value" ) ); 
/**hasItem匹配符表明被测的迭代对象iterableObject含有元素element项则测试通过*/ 
assertThat(iterableObject, hasItem (element)); 
/** hasKey匹配符断言被测的Map对象mapObject含有键值“key”*/ 
assertThat(mapObject, hasKey ("key")); 
/** hasValue匹配符断言被测的Map对象mapObject含有元素值value*/ 
assertThat(mapObject, hasValue(value));

assertThat举例:

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

public class HamcrestExamples {

  @Test
  public void allOfExampleShowsAllMatchersMustAllBeTrue() throws Exception {
    assertThat("Hello", is(allOf(notNullValue(), instanceOf(String.class), equalTo("Hello"))));
  }

  @Test
  public void allOfExampleShowsFailingIfOneMatcherDoesNotMatch() throws Exception {
    assertThat("Hello", is(not(allOf(notNullValue(), instanceOf(Integer.class)))));
  }

  @Test
  public void anyExampleChecksThatClassIsOfSameType() throws Exception {
    assertThat("Hello", is(any(String.class)));
  }

  @Test
  public void anyExampleShowsStringIsAlsoAnObject() throws Exception {
    assertThat("Hello", is(any(Object.class)));
  }

  @Test
  public void anyOfExampleReturnsTrueIfOneMatches() throws Exception {
    assertThat("Hello", is(anyOf(nullValue(), instanceOf(String.class), equalTo("Goodbye"))));
  }

  @Test
  public void anyOfExampleFailingIfAllMatchersAreFalse() throws Exception {
    assertThat("Hello", is(not(anyOf(nullValue(), instanceOf(Integer.class), equalTo("Goodbye")))));
  }

  @Test
  public void anythingExampleAlwaysReturnsTrue() throws Exception {
    assertThat("Hello", is(anything()));
  }

  // Feels very esoteric and not for typical usage used to override the description
  @Test
  public void describedAsExample() throws Exception {
    Matcher< ?> matcher = describedAs("My Description", anything());
    Description description = new StringDescription().appendDescriptionOf(matcher);
    assertThat("My Description", is(description.toString()));
  }

  @Test
  public void equalToExampleAddingTwoPlusTwo() throws Exception {
    assertThat(2 + 2, is(equalTo(4)));
  }

  @Test
  public void instanceOfExampleForString() throws Exception {
    assertThat("Hello", is(instanceOf(String.class)));
  }

  @Test
  public void isExampleShortCutForIsInstanceOfClass() throws Exception {
    assertThat("Hello", is(String.class));
    assertThat("Hello", instanceOf(String.class));
  }

  @Test
  public void isExampleShortCutAsJustSyntacticSugarUsedThreeTimes() throws Exception {
    assertThat("Hello", is(is(is(notNullValue()))));
  }

  @Test
  public void isExampleShortCutForIsEqualTo() throws Exception {
    assertThat("Hello", is("Hello"));
    assertThat("Hello", equalTo("Hello"));
  }

  @Test
  public void notExampleJustInvertsExpression() throws Exception {
    assertThat("Hello", is(not(instanceOf(Integer.class))));
  }

  @Test
  public void notNullValueExampleForString() throws Exception {
    assertThat("Hello", is(notNullValue()));
  }

  @Test
  public void notNullValueExampleForAClass() throws Exception {
    assertThat("Hello", is(notNullValue(Object.class)));
  }

  @Test
  public void nullValueExampleWithANull() throws Exception {
    assertThat(null, is(nullValue()));
  }

  @Test
  public void nullValueExampleWithANullType() throws Exception {
    Integer nothing = null;
    assertThat(nothing, is(nullValue(Integer.class)));
  }

  @Test
  public void sameInstanceExample() throws Exception {
    Object object = new Object();
    Object sameObject = object;
    assertThat(object, is(sameInstance(sameObject)));
  }
}

  
4. Test Suite的使用
在@Suite.SuiteClasses()中加入需要进行测试的类,例如Dice4Test.class。public class AllTests 里面留空,为的是编译器通过编译。

另外,在Eclipse中统计测试代码行覆盖率和分支覆盖率时,请使用EclEmma插件。
参考文献:
1. http://edgibbs.com/junit-4-with-hamcrest/
2. www.vogella.de/articles/JUnit/article.html 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值