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

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

问题描述

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.Assert org.junit.Assert

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

推荐答案

旧方法(JUnit 3)是通过扩展 junit.framework.TestCase 。这继承了 junit.framework.Assert 本身,你的测试类获得了以这种方式调用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 Assert类之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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