2 个 JUnit 断言类之间的差异 [英] differences between 2 JUnit Assert classes

查看:33
本文介绍了2 个 JUnit 断言类之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JUnit 框架包含 2 个 Assert 类(显然在不同的包中)并且每个类的方法看起来非常相似.谁能解释一下这是为什么?

The JUnit framework contains 2 Assert classes (in different packages, obviously) and the methods on each appear to be very similar. Can anybody explain why this is?

我所指的类是:junit.framework.Assertorg.junit.Assert.

The classes I'm referring to are: junit.framework.Assert and org.junit.Assert.

推荐答案

(JUnit 3 的)旧方法是通过扩展 junit.framework.TestCase 来标记测试类.继承的 junit.framework.Assert 本身和您的测试类获得了以这种方式调用断言方法的能力.

The old method (of JUnit 3) was to mark the test-classes by extending junit.framework.TestCase. That inherited junit.framework.Assert itself and your test class gained the ability to call the assert methods this way.

从 JUnit 的第 4 版开始,该框架使用 Annotations 来标记测试.所以你不再需要扩展TestCase.但这意味着,断言方法不可用.但是您可以静态导入新的 Assert 类.这就是为什么新类中的所有断言方法都是静态方法的原因.所以你可以这样导入:

Since version 4 of JUnit, the framework uses Annotations for marking tests. So you no longer need to extend TestCase. But that means, the assert methods aren't available. But you can make a static import of the new Assert class. That's why all the assert methods in the new class are static methods. So you can import it this way:

import static org.junit.Assert.*;

在这个静态导入之后,你可以不用前缀使用这个方法.

After this static import, you can use this methods without prefix.

在重新设计时,他们还转移到了新的包 org.junit,它更好地遵循了包命名的正常约定.

At the redesign they also moved to the new package org.junit that follows better the normal conventions for package naming.

这篇关于2 个 JUnit 断言类之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆