Junit 包中有两个Assert 类,分别是:
org.junit.Assert【推荐使用】
package org.junit;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.junit.internal.ArrayComparisonFailure;
import org.junit.internal.ExactComparisonCriteria;
import org.junit.internal.InexactComparisonCriteria;
public class Assert {
}
org.framework.Assert
package junit.framework;
/**
* A set of assert methods. Messages are only displayed when an assert fails.
*
* @deprecated Please use {@link org.junit.Assert} instead.
*/
@Deprecated
public class Assert {
}
可以看到,org.framework.Assert 官方已经不推荐使用,所以在单元测试中使用断言的时候,要用第一个,而且第一个 Assert 有一个很强大的方法:assertThat。
最常用的几个方法如下:
String message = "如果condition不成立,打印当前message 并 抛出 AssertionError 错误";
boolean condition = count1 < count2;
Assert.assertTrue(message, condition);
String message = "如果condition成立,打印当前message 并 抛出 AssertionError 错误";
String result = "0000";
boolean condition = result.equals("9999");
Assert.