Assert.assertEquals作用

junit.framework包下的Assert提供了多个断言方法. 主用于比较测试传递进去的两个参数.

Assert.assertEquals();及其重载方法: 1. 如果两者一致, 程序继续往下运行. 2. 如果两者不一致, 中断测试方法, 抛出异常信息 AssertionFailedError .

查看源码, 以Assert.assertEquals(int expected, int actual)为例:

/**
 * Asserts that two ints are equal. 断言两个int是相等的
 */
static public void assertEquals(int expected, int actual) {
    assertEquals(null, expected, actual);
}

可以看到里面调用了assertEquals(String message, int expected, int actual)方法:

/**
 * Asserts that two ints are equal. If they are not
 * an AssertionFailedError is thrown with the given message.
 * 如果不抛出带有 message 的异常(AssertionFailedError)信息, 则表明两者相等
 */
static public void assertEquals(String message, int expected, int actual) {
    assertEquals(message, Integer.valueOf(expected), Integer.valueOf(actual));
}

可以看到, 这里把int类型封箱成为Integer类型. 注释说, 会抛异常, 但这里没有. 没关系, 我们接着看里面调用: assertEquals(String message, Object expected, Object actual)方法:

/**
 * Asserts that two objects are equal. If they are not
 * an AssertionFailedError is thrown with the given message.
 * 如果不抛出带有 message 的异常(AssertionFailedError)信息, 则表明两者相等(这里比较的是Object对象)
 */
static public void assertEquals(String message, Object expected, Object actual) {
    if (expected == null && actual == null) {
        return;
    }
    if (expected != null && expected.equals(actual)) {
        return;
    }
    failNotEquals(message, expected, actual);
}

两个if语句, 判断了两者相等的情况: 引用(地址)相等或者内容相等. 如果这两种if情况都不命中, 那么表明1参和2参实际是不相等, 所以代码会往下执行failNotEquals(String message, Object expected, Object actual)方法,并在此方法中抛出异常, 接下来就比较简单了:

static public void failNotEquals(String message, Object expected, Object actual) {
    fail(format(message, expected, actual));
}

public static String format(String message, Object expected, Object actual) {
    String formatted = "";
    if (message != null && message.length() > 0) {
        formatted = message + " ";
    }
    return formatted + "expected:<" + expected + "> but was:<" + actual + ">";
}
/**
* Fails a test with the given message.
*/
static public void fail(String message) {
	if (message == null) {
	    throw new AssertionFailedError();
	}
	throw new AssertionFailedError(message);
}

以上可以看出, 最终是由fail(String message)这个方法抛出异常信息!!

Assert.assertEquals()使用方法:
使用, 示例代码:

Assert.assertEquals(true, arry.contains("hello"));
Assert.assertEquals(39991L, aa.getLong("key3", 0L));
Assert.assertEquals(true, bb.getBoolean("key4", false));
Assert.assertEquals(5.3f, cc.getFloat("key5", 0.f));
Assert.assertEquals(99, dd.getInt("key6", 1));
Assert.assertEquals("如果打印本信息, 证明参数不相等", 10L, 10);

按照源码分析, 我们可以把一个预期结果作为1参传递进去. 2参传递我们需要测试的方法. 然后执行. 相等, 代码继续往下执行, 不相等, 中断执行, 抛出异常信息!!!

略作一提:
Assert.assertSame(Object expected, Object actual)方法:
查看源码, 其比较的是引用地址是否相等, 并没有对内容进行比较:

/**
 * Asserts that two objects refer to the same object. If they are not
 * the same an AssertionFailedError is thrown.
 */
static public void assertSame(Object expected, Object actual) {
    assertSame(null, expected, actual);
}
/**
 * Asserts that two objects refer to the same object. If they are not
 * an AssertionFailedError is thrown with the given message.
 */
static public void assertSame(String message, Object expected, Object actual) {
    if (expected == actual) {
        return;
    }
    failNotSame(message, expected, actual);
}
  • 56
    点赞
  • 137
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据立方体是复杂计算的抽象。Datacube 是用 Java 实现的,可插入数据库后端支持的数据立方体。 datacube 是用来存储大数据点的聚合信息。数据立方体存储的是有趣输入数据点的子集。比如,你正在编写一个 web 服务器日志分析工具,你的输入点可能是日志行,你可能会计算每个浏览器的型,每个浏览器的版本,操作系统型,操作系统版本和其他属性。同时你可能会需要计算一个特定的组合计数(浏览器型,浏览器版本,操作系统型), (浏览器型,浏览器版本,操作系统型,操作系统版本),等等。 这对快速添加和修改计数是个很大的挑战,会浪费很多时间在数据库代码和重新用新计数器处理旧数据。而数据立方体就可以帮忙解决这些问题。 Urban Airship 使用 datacube 项目来支持他们的移动端应用的分析栈,每个节点每秒处理大约 10 K 的事件。 datacube 要求 JDK 1.6。 特性 性能: 高速异步 IO 后端处理 使用 Hadoop MapReduce 进行批量加载 可插入数据库接口 datacube 暂时只支持 HBase 数据库后端。 示例: IdService idService = new CachingIdService(5, new MapIdService()); ConcurrentMap backingMap =          new ConcurrentHashMap(); DbHarness dbHarness = new MapDbHarness(backingMap, LongOp.DESERIALIZER,          CommitType.READ_COMBINE_CAS, idService); HourDayMonthBucketer hourDayMonthBucketer = new HourDayMonthBucketer(); Dimension time = new Dimension("time", hourDayMonthBucketer, false, 8); Dimension zipcode = new Dimension("zipcode", new StringToBytesBucketer(),          true, 5); DataCubeIo cubeIo = null; DataCube cube; Rollup hourAndZipRollup = new Rollup(zipcode, time, HourDayMonthBucketer.hours); Rollup dayAndZipRollup = new Rollup(zipcode, time, HourDayMonthBucketer.days); Rollup hourRollup = new Rollup(time, HourDayMonthBucketer.hours); Rollup dayRollup = new Rollup(time, HourDayMonthBucketer.days); List dimensions =  ImmutableList.of(time, zipcode); List rollups = ImmutableList.of(hourAndZipRollup, dayAndZipRollup, hourRollup,         dayRollup); cube = new DataCube(dimensions, rollups); cubeIo = new DataCubeIo(cube, dbHarness, 1, Long.MAX_VALUE, SyncLevel.FULL_SYNC); DateTime now = new DateTime(DateTimeZone.UTC); // Do an increment of 5 for a certain time and zipcode cubeIo.writeSync(new LongOp(5), new WriteBuilder(cube)         .at(time, now)         .at(zipcode, "97201"))
ANGROM 是一个简单的android 数据库 dsl, 解决了数据库版本控制问题, 提供了select查询. 使用入门 下载并导入jar包 创建MyApplication public class MyApplication extends Application {   @Override   public void onCreate() {       super.onCreate();   }} 修改 AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"   package="xx.xx.xx"   android:versionCode="1"   android:versionName="1.0" >   <application       android:name=".MyApplication"       android:allowBackup="true"       android:icon="@drawable/ic_launcher"       android:label="@string/app_name"       android:theme="@style/AppTheme" >   </application></manifest> 创建数据库 public class MyApplication extends Application {   @Override   public void onCreate() {     super.onCreate();     MigrationManager migrationManager = new MigrationManager();     Migration user = new CreateTable("users")                .addAutoIncrementPrimaryKey("id")                .addColumn(Genre.STRING, "name");     migrationManager.addMigration(1, user); // 添加数据库1的迁移, 可以添加很多个     SQLHelper sqlHelper = new SQLHelper(getContext(), migrationManager, "test.db", 1);     sqlHelper.getWritableDatabase();   }} 5.修改版本 public class MyApplication extends Application {   @Override   public void onCreate() {     super.onCreate();     MigrationManager migrationManager = new MigrationManager();     Migration user = new CreateTable("users")                .addAutoIncrementPrimaryKey("id")                .addColumn(Genre.STRING, "name");     migrationManager.addMigration(1, user); // 添加数据库1的迁移, 可以添加很多个     Migration person = new CreateTable("person")                 .addAutoIncrementPrimaryKey("id")                 .addColumn(Genre.STRING, "age");     migrationManager.addMigration(2, person);     Migration test1 = new CreateTable("test1")             .addAutoIncrementPrimaryKey("id")             .addColumn(Genre.INTEGER, "age");     migrationManager.addMigration(3, test1);     Migration test2 = new CreateTable("test2")             .addAutoIncrementPrimaryKey("id")             .addColumn(Genre.INTEGER, "age");     migrationManager.addMigration(4, test2);     SQLHelper sqlHelper = new SQLHelper(getContext(), migrationManager, "test.db", 4);     sqlHelper.getWritableDatabase();   }} 使用查询 Cursor cursor = new Select("id, name")                 .from("users")                 .where(String.format("id=%s", id_.toString()))                 .query(sqlHelper.getReadableDatabase());if (cursor.moveToNext()) {     Assert.assertEquals(cursor.getString(1), "ngdkSelect");} else {     Assert.fail("测试失败");}Cursor cursor = new Select("id, name")                 .from("users")                 .where("id=?")                 .query(sqlHelper.getReadableDatabase(), new String[] {id.toString()});if (cursor.moveToNext()) {     Assert.assertEquals(cursor.getString(1), "ngdkSelect");} else {     Assert.fail("测试失败");} 标签:ANGROM

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值